Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

case when in where clause oracle 11g

Posted on 2011-10-05
12
Medium Priority
?
429 Views
Last Modified: 2012-05-12
   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      
0
Comment
Question by:cookiejar
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1000 total points
ID: 36919486
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36919505
You might also want a '>=' to account for the time portion of the date data type in Oracle.
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 1000 total points
ID: 36919651
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:cookiejar
ID: 36919669
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36919672
I knew there was an 'easy' way.  Just couldn't think of it.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36919689
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
 
LVL 74

Expert Comment

by:sdstuber
ID: 36919695
ah,  you already caught the null problem.

just use the OR, and skip CASE
0
 
LVL 32

Expert Comment

by:awking00
ID: 36924349
and nvl(emp_eaos,in_date + 1) > in_date
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36924365
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
 

Author Comment

by:cookiejar
ID: 36927146
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36927165
Generate the execution plan and see:

explain plan for
select emp_table
      from table
      where ...

select * from table(dbms_xplan.display);
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36927477
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question