Solved

CASE statement in WHERE clause

Posted on 2006-10-22
5
12,489 Views
Last Modified: 2008-01-09
Can i write the following query:

SELECT a,b,c FROM my_table
WHERE    CASE in_emp_id = 0 AND date_from <> 0 THEN exh_valid_from = in_date_from
              , CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_emp_id = in_emp_id
              , CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from AND exh_emp_id = in_emp_id;

in_date_from and in_emp_id are input parameters passed to the procedure which contains this query.
exh_emp_id and exh_valid_from are dates from my_table.
Can someone help me out

Thanks
0
Comment
Question by:aaba646
5 Comments
 
LVL 19

Accepted Solution

by:
actonwang earned 168 total points
ID: 17786188
I think that what you want is like :

SELECT a,b,c FROM my_table
WHERE    (in_emp_id = 0 AND date_from <> 0 and exh_valid_from = in_date_from ) or
              (in_emp_id <> 0 AND in_date_from <> 0 and exh_emp_id = in_emp_id) or
              (CASE in_emp_id <> 0 AND in_date_from <> 0 and exh_valid_from = in_date_from AND exh_emp_id = in_emp_id)
/

right :)
Acton
0
 
LVL 14

Assisted Solution

by:GGuzdziol
GGuzdziol earned 166 total points
ID: 17787295
I think You want

SELECT a,b,c
  FROM my_table
  WHERE (in_emp_id = 0 OR exh_emp_id = in_emp_id)
    AND (in_date_from = 0 OR xh_valid_from = in_date_from);
0
 
LVL 18

Assisted Solution

by:Jinesh Kamdar
Jinesh Kamdar earned 166 total points
ID: 17791548
Your 2nd & 3rd CASE conditions do not make sense since they evaluate the same conditions, but return diff. results : -

, CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_emp_id = in_emp_id
, CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from AND exh_emp_id = in_emp_id;

I suppose they shud be as below : -

  CASE in_emp_id = 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from
, CASE in_emp_id <> 0 AND in_date_from = 0 THEN exh_emp_id = in_emp_id
, CASE in_emp_id <> 0 AND in_date_from <> 0 THEN exh_valid_from = in_date_from AND exh_emp_id = in_emp_id;

If i understand ur requirement correctly, u have a procedure that has 2 IN parameters - in_emp_id, in_from_date
that u want to compare to ur table values.

Wat u need is as below : -

SELECT a, b, c FROM my_table
WHERE DECODE(in_emp_id,     0, exh_emp_id,     in_emp_id)     = exh_emp_id
AND     DECODE(in_date_from, 0, exh_valid_from, in_date_from) = exh_valid_from
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20296184
Forced accept.

Computer101
EE Admin
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now