Case in where clause

I have a store procedure and what I am trying to do is to say if the state eq 4 then exclude type DC.

WHERE SM.officeStateKey = @stateKey
AND CASE WHEN SM.officeStateKey = 4
		THEN  SM.type != 'DC'
		END type

Open in new window

LVL 19
erikTsomikSystem Architect, CF programmer Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

dsackerContract ERP Admin/ConsultantCommented:
Try this:
WHERE SM.officeStateKey = @stateKey
AND  (SM.type <> 'DC' OR SM.officeStateKey <> 4)

Open in new window


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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
This is more obvious, though dsacker's condition is correct:
WHERE SM.officeStateKey = @stateKey
AND (SM.officeStateKey = 4 and SM.type != 'DC'
        or SM.officeStateKey != 4)

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Re your CASE: always keep in mind the whole CASE is no condition (as an IF), but an expression. It has to result in a value.
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
< No points for this comment, as the question has already been answered, but if you read my article and 'like' it that would be super.. >

For an image and code-heavy tutorial on CASE blocks please read my article on SQL Server CASE Solutions.
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
Query Syntax

From novice to tech pro — start learning today.