Mahajan344
Mahajan344

Reputation: 2550

case statement in where clause with in values oracle sql

I am working on Oracle Apex application. I have a simple select query which is working fine but now I want to do if/else or case statement in that query.

so query is below

select value d,
       id r
  from dhm_lov_data
 where lov_id = 103
   and nvl(isdeleted,0) = 0
   and parentid_1 =:p21_typeid
 order by d

I want to change this query to add a condition on :P21_TYPEID value. E.g. if :P21_TYPEID value is 1 then in where clause it should be PARENTID_1 in (10,11) otherwise PARENTID_1 = :P21_TYPEID.

I tried to put it in case statement but it didn't work. Below is query I am trying to make work using a case statement

SELECT  VALUE d, id r FROM DHM_LOV_DATA WHERE LOV_ID= 103 and nvl(ISDELETED,0) = 0  
and 
case when :P21_TYPEID = 848 then PARENTID_1 in (287,288)
    else PARENTID_1 = :P21_TYPEID  end;
order by d

Upvotes: 1

Views: 1937

Answers (2)

Littlefoot
Littlefoot

Reputation: 142713

Something like this ugly example, perhaps?

select  value d, id r 
from dhm_lov_data 
where lov_id = 103 
  and nvl(isdeleted, 0) = 0  
  and parentid_1 in (select :p21_typeid from dual
                     where :p21_typeid <> 848
                     union
                     select val from (select 287 val from dual 
                                      union
                                      select 288 val from dual
                                     )
                     where :p21_typeid = 848
                    )

Upvotes: 0

Mureinik
Mureinik

Reputation: 311308

A case expression returns a value, not a condition you could just add to your where clause. But you could emulate this behavior with some boolean operators:

SELECT   value d, id r 
FROM     dhm_lov_data
WHERE    lov_id = 103 AND
         NVL(isselected, 0) = 0 AND
         (:P21_TYPEID = 848 AND parentid_1 IN (287,288) OR
          :P21_TYPEID = parentid_1)
ORDER BY d

Upvotes: 3

Related Questions