Mr.Tananki
Mr.Tananki

Reputation: 489

Conditional Statements in PostgreSQL

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

Upvotes: 1

Views: 409

Answers (2)

Mr.Tananki
Mr.Tananki

Reputation: 489

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 

Upvotes: 0

ChrisB
ChrisB

Reputation: 2565

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');

Upvotes: 2

Related Questions