Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Placing a CASE statement in a WHERE Clause

Status
Not open for further replies.

LeonelSanchezJr

Programmer
Jan 26, 2001
522
US
I need to apply certain filters to my WHERE clause
depending on the certain conditions which I believe can be met by a CASE statement.

example:

SELECT *
FROM TABLE
WHERE TABLE.COUNTRY = 'ABC'

(CASE WHEN TABLE.INVTYPE = '999'
THEN AND TABLE.ARTYPE <> '555'
END
)
;

The error I get is:
SQL Command not properly ended.

Thanks,

Leo ;-)
 
Leo,

Perhaps I'm missing something, but can you not simply say:
Code:
SELECT *
FROM TABLE
WHERE TABLE.COUNTRY = 'ABC'       
  AND TABLE.INVTYPE = '999'
  AND TABLE.ARTYPE <> '555';
We use the CASE construct as a more intuitive DECODE function. In your example, the CASE clause is syntactically incomplete.

Here is a contrived example of a CASE clause in a WHERE statement (to illustrate working code):
Code:
select last_name, salary from s_emp
where CASE WHEN salary > 1500 then 'A'
           WHEN salary < 1000 then 'B'
      END in ('A','B');

LAST_NAME                     SALARY
------------------------- ----------
Velasquez                     3327.5
Ropeburn                        1550
Sedeghi                         1515
Nguyen                          1525
Smith                            940
Patel                            795
Newman                           750
Chang                            800
Patel                            795
Dancs                            860

10 rows selected.
Let us know if this helps resolve your need.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I can provide you with low-cost, remote Database Administration services: see our website and contact me via www.dasages.com]
 

The WHERE clause is already a conditional clause, Why embed another conditional which could be satisfieds with logical operators instead of a CASE statement (even if it's allowed)?

Besides, the CASE clause you posted is misconstrued, to be valid, you would need something like this:

Code:
SELECT *
FROM TABLE
WHERE TABLE.COUNTRY = 'ABC'       
 [red]AND SomeColumn = [/red]
(CASE WHEN TABLE.INVTYPE = '999'
       AND TABLE.ARTYPE <> '555'
 THEN [red]'SOME RESULT'
 ELSE 'What?'[/red]
 END);

[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
This situation is quite strange because I need to apply 2 different rules.

When the INVTYPE = '999'
then, I DO NOT want to include ARTYPEs of '555'
Otherwise, I do want to indluce ARTYPES of '555'


Thanks,

Leo ;-)
 
Code:
when (INVTYPE != '999')
or   (INVTYPE = '999' and ARTYPE != '555')



Beware of false knowledge; it is more dangerous than ignorance. ~George Bernard Shaw
Consultant Developer/Analyst Oracle, Forms, Reports & PL/SQL (Windows)
My website: Emu Products Plus
 
I still believe that you do not need a CASE statement...just say:
Code:
...WHERE (invtype  = '999' AND artype != '555')
      OR (invtype != '999' AND artype  = '555')
Right?

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I can provide you with low-cost, remote Database Administration services: see our website and contact me via www.dasages.com]
 
of course I meant WHERE...

Beware of false knowledge; it is more dangerous than ignorance. ~George Bernard Shaw
Consultant Developer/Analyst Oracle, Forms, Reports & PL/SQL (Windows)
My website: Emu Products Plus
 
WHEN"..."WHERE"...Besides Oracle/SQL, BJ, who really cares? [wink]

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I can provide you with low-cost, remote Database Administration services: see our website and contact me via www.dasages.com]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top