Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Everbody,

How can I use CASE WHEN in WHERE Clause Please find my below query::

SQL
SELECT C.*
FROM COM C
LEFT OUTER JOIN ROSTER R ON R.EMPID = C.EMPID
LEFT OUTER JOIN USER U ON U.EMPID = R.EMPID
WHERE TID = (CASE WHEN C.TID IS NULL OR C.TID = '' THEN C.TTID
             WHEN R.TID IS NULL THEN U.TID ELSE R.TID END)


How can I achieve this.? Is it possible like this.?
Posted
Updated 10-Aug-15 11:26am
v2
Comments
[no name] 10-Aug-15 4:40am    
If you provide more information about what kind of data you need to fetch and why you require case in where clause
then may be some one helps

1 solution

Probably, you need to clarify which TID value you are trying to access.
SQL
WHERE TID = (CASE WHEN ...
Doesn't specify, but the rest of the code:
SQL
C.TID IS NULL ...  WHEN R.TID IS NULL ...
Shows two values with the same name in different tables.
I'm just guessing you want
SQL
WHERE U.TID = (CASE WHEN ...
 
Share this answer
 
Comments
tpkpradeep 10-Aug-15 6:19am    
DECLARE @TID CHAR(2)
SET @TID = 'ON'

WHERE @TID = (CASE WHEN C.TID IS NULL OR C.TID = '' THEN C.TTID
WHEN R.TID IS NULL THEN U.TID ELSE R.TID END)

Sorry I forgot to add the above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900