How to using select with if condition in oracle?

swathy picture swathy · Mar 4, 2011 · Viewed 48.1k times · Source

My requirement is to get a number from a complex query and check if the num = desiredNum.

If it is equal to desiredNum, then I must perform another set of select statements,

Is there any way I can achieve this in a query rather than writing a function?

Eg:

select case when val =2  
then select val1 from table1  
else 'false'  
from (select val from table)  

Is this possible ??

Answer

RichardTheKiwi picture RichardTheKiwi · Mar 4, 2011
select case when val=2 then val1 else val end as thevalue
from table1

I assume you meant that val and val1 are both from the same table, but when val=2, to use val1 instead. If you actually had two tables, and they both have only one record each, then

select
    case when val=2
    then (select val1 from table1)
    else 'false'
    end
from table