?
Solved

CASE statement in WHERE clause

Posted on 2006-10-22
5
Medium Priority
?
12,753 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
4 Comments
 
LVL 19

Accepted Solution

by:
actonwang earned 672 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 664 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 664 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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