0

Can we use switch case to filter the columns of a table in this way?

select distinct stud.name, stud.num,
        case WHEN stud.sub like '%data%'            THEN    stud.sub
             WHEN stud.sub like '%informatics%'     THEN    stud.sub
             WHEN stud.sub like '%genomics%'        THEN    stud.sub
                                                    ELSE    '---'
        END
from  table_A

The expected result is

Name    ID      Sub

victor  2098    -----
Lui     6754    Bioinformatics
Willis  7653    Advanceddatascience

Thanks!

koolone
  • 1
  • 1

1 Answers1

0

Yes, your query should work. If you want to try something different, this should work in Oracle. Easy to change it to any other RDBMS. Also easy to add any other subject.

With aux as ( select '%data%' as auxText from dual union all
              select '%informatics%' as auxText from dual union all            
              select '%genomics%' as auxText from dual )
select distinct stud.name, stud.num,
        nvl(stud.sub,'---') as sub
from  table_A left join aux on table_A.sub like aux.auxText;
vercelli
  • 4,717
  • 2
  • 13
  • 15