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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimize case when to use IN? 1

Status
Not open for further replies.

Qik3Coder

Programmer
Jan 4, 2006
1,487
US
I have a status column that contains text.
I have to return either everything, or where the status is in(X) based on whether or not a flag was sent

Code:
and (i.status = case when @ActiveOnly = 1 then 'OPEN' else i.status END
or
i.status = case when @ActiveOnly = 1 then 'PEND' else i.status END)

is there a way to mix the case when and an IN statement?

-Sometimes the answer to your question is the hack that works
 
From the looks of it I don't think you need CASE at all. From the looks of it you are just joining on i.status = i.status, I don't think this is what you want?

try something like

Code:
and ((@ActiveOnly = 1 AND i.status in ('PEND', 'END')) OR @ActiveOnly <> 1)

(not tested)

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
The IT Community of the 21st Century
 
Thanks Alex, with the way things have been going today I think I left my brain at the house...

-Sometimes the answer to your question is the hack that works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top