How to get multiple columns in a single SQL CASE statement?

Michael.Y picture Michael.Y · Oct 9, 2016 · Viewed 63.2k times · Source

I'm trying to get multiple columns(insuredcode, insuredname in this case) from a single CASE statement.

The following query has been tried but it concatenates both insuredcode and insuredname as one column.

What is the correct syntax to return exactly two columns from such CASE statement?

select
    case
        when a.policyno[2] in ('E', 'W') then c.insuredcode || c.insuredname
        else b.insuredcode || b.insuredname
    end
from prpcmain a
left join prpcinsured_1 b on b.proposalno=a.proposalno 
left join prpcinsured_2 c on c.proposalno=a.proposalno
where a.policyno in (select policyno from policyno_t);

Answer

Pரதீப் picture Pரதீப் · Oct 9, 2016

A CASE statement can return only single column not multiple columns

You need two different CASE statements to do this

select
    case
        when a.policyno[2] in ('E', 'W') then c.insuredcode 
        else b.insuredcode 
    end as insuredcode ,
    case
        when a.policyno[2] in ('E', 'W') then  c.insuredname
        else b.insuredname
    end as insuredname
from prpcmain a
left join prpcinsured_1 b on b.proposalno=a.proposalno 
left join prpcinsured_2 c on c.proposalno=a.proposalno
where a.policyno in (select policyno from policyno_t);