1

A 'select' statement produce some sequence of data output like 'SL' 'CL''PL' etc as shown in below picture enter image description here

If the data output is SL then it should be converted and displayed as 'Z', if its 'CL' the 'Q', if its 'PL' then 'R'

Is there any way to implement this

1

2 Answers 2

2

You can use CASE WHEN

select case when dbreed1 = 'SL' then 'Z'
            when dbreed1 = 'CL' then 'Q'
            when dbreed1 = 'PL' then 'R'
            else 'other'
       end dbreed1_mapped

You can also write your own function:

create or replace function map_breed(_input text) returns text as
$$
    select case when _input = 'SL' then 'Z'
                when _input = 'CL' then 'Q'
           end
$$
language sql;

  And call it like this:

select map_breed('SL');
Sign up to request clarification or add additional context in comments.

4 Comments

I could but is there any better way than that, like 'for' loop for the whole columns? so that no need to write for individual one
@Mr.Tananki I edited my answer so you only have to do the mapping once in the function
writing own function worked after struggling to handel the following error.
ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function "map_breed" line 2 at SQL statement
0
create or replace function map_breed(dbreed text) returns text as 
$$ 
   begin 
   select case when dbreed = 'SL' then 'Z' 
               when dbreed = 'CL' then 'Q' end into dbreed; 
   return dbreed;
   end; 
$$ language plpgsql; 

/*and call the function as*/
select map_breed('SL') as breed 

1 Comment

A plain SQL function would be more efficient for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.