Link to home
Start Free TrialLog in
Avatar of StarDusterII
StarDusterII

asked on

How to write conditional where clause with CASE statement

I have a stored procedure that I'm passing a value in the variable @status and I want to do conditional logic to create the proper where clause.  If @status is 0 then the where clause should be: where status_int <> 9.  If it's 1 then the where clause is: where status_int=2.  If it's 2 then the where clause is: where status_int in (1,-1).

I've written something like this but it gives a syntax error... oddly enough on the "<" and the second "when"

select * from license
where case @status
when 0 then status_int <> 9
when 1 then status_int = 2
when 2 then status_int in (1,-1)
end

I've tried other variations but just can't seem to get it right.  How can I fix this?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of StarDusterII
StarDusterII

ASKER

Yeah... that worked.  Don't know why but I was hoping a case statement would do it.   Thanks for the help.