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?
StarDusterIIAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
case can only be used as a conditional expression, not condition itself. You have to stay with something like:
select * from license 
where (@status = 0 and status_int <> 9)
     or (@status = 1 and status_int = 2)
    or (@status = 2 and status_int in (1,-1))

Open in new window

You don't need the parens, as AND has a higher priority than OR, but the meaning is more clear that way.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StarDusterIIAuthor Commented:
Yeah... that worked.  Don't know why but I was hoping a case statement would do it.   Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.