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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Skip conditions in where clause besed on parameter
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 03/25/2013 :  07:59:43  Show Profile  Reply with Quote
Hi Team,
I am not an expert in SQL programming & reports. I have typical requirement in report creation having 4 multi value conditions and date(between) which are selected as parameters while generation of Report.
all the four parameters are to be selected from a list (can have multiple values selected) and the date as start date to end date. In all the 4 conditions there will be one option as "ALL" rest will be values as usual like "Started","Pending","waiting","Approved","closed"(these are DB valied values except "ALL"). etc
my problem here is:
So if the user selects "ALL" as a parameter value in the list along with other values or selects only "ALL" then this condition should be skipped and rest of the conditions must proceed and similarly if any of the 4 conditional parameters contain "ALL", that respective condition should be avoided.
example :
select x,y,z from Table
where x in ("ALL","Pending","Closed") and y in ("Dept1", "Dept2", "Dept4") and z in ("ALL","Asia","Europe")

here the conditions x and z are to be skipped or avoided in the query since they contian "ALL" as one value.

Can some one plese tell me the best possible way to conditionally skip conditions(multiple) in where clause.

Thanks in advance.

Acharya RK

Flowing Fount of Yak Knowledge

2242 Posts

Posted - 03/25/2013 :  08:48:03  Show Profile  Reply with Quote
May be this?

where (x in ("ALL","Pending","Closed") OR X LIKE '%ALL%')
   and (y in ("Dept1", "Dept2", "Dept4")  OR Y LIKE '%ALL%')
   and (z in ("ALL","Asia","Europe")  OR Z LIKE '%ALL%')

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 03/25/2013 :  08:55:09  Show Profile  Reply with Quote
You can do something like shown below, assuing the parameters are @xparam, @yparam and @zparam and assuming they are comma-separated values:
select x,y,z from Table
	(','+@xparam+',' like '%,ALL,%' or ','+@xparam+',' like '%,'+x+',%')
	(','+@yparam+',' like '%,ALL,%' or ','+@yparam+',' like '%,'+y+',%')
	(','+@zparam+',' like '%,ALL,%' or ','+@zparam+',' like '%,'+y+',%')
This may have performance issues - take a look at this page if you do:
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000