Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 conditional in where clause

Author  Topic 

bill_
Starting Member

38 Posts

Posted - 2011-03-14 : 17:18:59
We have a parameter @stts that can have values 'Active','Closed','Both' and a field called stts that is char(25).
We need to put this logic in a where clause right after the last "and"

if (@stts='Active' and substring(stts,2,1)='')
or (@stts='Closed' and substring(stts,2,1)<>'')
then
true
else
false
end

How is that done?

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2011-03-15 : 00:46:13
Use CASE statement
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2011-03-15 : 00:49:52
Something like:

Declare @stts varchar(25)

Select * from sys.objects where 1 = 1
and --Your last end statement
Case When (@stts='Active' and substring(stts,2,1)='') or (@stts='Closed' and substring(stts,2,1)<>'') Then 1
Else 0 End = 0
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-15 : 10:32:20
and
((@stts='Active' and substring(stts,2,1)='') or (@stts='Closed' and substring(stts,2,1)<>''))


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -