Avatar of cookiejar
cookiejarFlag for United States of America

asked on 

case when in where clause oracle 11g

   It may be another way of doing this
    I would like to be able to evaluate on a flag that is sent by an interface to determine to filter by  a date or not to filter by a date by using the case when
   
    select emp_table
      from table
      where emp_assignment_status = 'ACTIVE'
         and emp_dept like '%MTN%'
         and
             -- determine if I should filter by date or not
               case in_flag  -- parameter  valuse passed  from a java inteface
                  when 'on' then
                     emp_eoas > to_date('01/01/2012','MM/DD/YYYY')
                  when 'off'  then
                     -- do not filter by date
                end      
Oracle Database

Avatar of undefined
Last Comment
Sean Stuber
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

You might also want a '>=' to account for the time portion of the date data type in Oracle.
SOLUTION
Avatar of Sean Stuber
Sean Stuber

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of cookiejar
cookiejar
Flag of United States of America image

ASKER

How would I modify to bring records that do not have a date value (null) when the in_flag is set to 'no'.

To pass the date value to a dynamic sql,
Would I define it like this:  in_date in DATE

Would I insert in my seql statement like this:

select emp_table
      from table
      where emp_assignment_status = 'ACTIVE'
         and emp_dept like '%MTN%'
         and emp_eoas > case in_flag when 'on' then in_date
           else   to_date('0001','YYYY') end

I knew there was an 'easy' way.  Just couldn't think of it.
Avatar of Sean Stuber
Sean Stuber

note,  SQL does short circuit boolean evaluation

so putting   in_flag='off'    means it won't even look at the column condition if flag is off.

this is not just an efficiency thing,  it also affects the results...

this :

>>>  and emp_eoas > case in_flag when 'on' then to_date('01/01/2012','MM/DD/YYYY') else   to_date('0001','YYYY') end

won't work  if emp_eoas  is nullable.
Avatar of Sean Stuber
Sean Stuber

ah,  you already caught the null problem.

just use the OR, and skip CASE
Avatar of awking00
awking00
Flag of United States of America image

and nvl(emp_eaos,in_date + 1) > in_date
Avatar of Sean Stuber
Sean Stuber

using NVL like that will most likely prevent use of any indexes on emp_eaos
also how does that handle the on/off  parameter?
Avatar of cookiejar
cookiejar
Flag of United States of America image

ASKER

So which is more efficient  is this one?
 select emp_table
      from table
      where emp_assignment_status = 'ACTIVE'
         and emp_dept like '%MTN%'
         and ( in_flag='off'
                 or
                  emp_eoas > to_date('01/01/2012','MM/DD/YYYY')
Generate the execution plan and see:

explain plan for
select emp_table
      from table
      where ...

select * from table(dbms_xplan.display);
Avatar of Sean Stuber
Sean Stuber

run them both and look at statistics,  autotrace is easiest.

time them

I think the version I posted should be the most efficient, but test for yourself.
Oracle Database
Oracle Database

Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo