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      
cookiejarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
You cannot declare a dynamic where clause this way in Oracle.  You need to use dynamic SQL if you get very complicated.

This should give you the correct results but may not be the most efficient:

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightwv (䄆 Netminder) Commented:
You might also want a '>=' to account for the time portion of the date data type in Oracle.
0
sdstuberCommented:
why use CASE at all?

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')
           
             
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

cookiejarAuthor Commented:
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

0
slightwv (䄆 Netminder) Commented:
I knew there was an 'easy' way.  Just couldn't think of it.
0
sdstuberCommented:
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.
0
sdstuberCommented:
ah,  you already caught the null problem.

just use the OR, and skip CASE
0
awking00Commented:
and nvl(emp_eaos,in_date + 1) > in_date
0
sdstuberCommented:
using NVL like that will most likely prevent use of any indexes on emp_eaos
also how does that handle the on/off  parameter?
0
cookiejarAuthor Commented:
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')
0
slightwv (䄆 Netminder) Commented:
Generate the execution plan and see:

explain plan for
select emp_table
      from table
      where ...

select * from table(dbms_xplan.display);
0
sdstuberCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.