SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

ranga.rav
Starting Member

India
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

bandi
Flowing Fount of Yak Knowledge

India
2217 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%')


--
Chandu
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3706 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
where
	(','+@xparam+',' like '%,ALL,%' or ','+@xparam+',' like '%,'+x+',%')
	and
	(','+@yparam+',' like '%,ALL,%' or ','+@yparam+',' like '%,'+y+',%')
	and
	(','+@zparam+',' like '%,ALL,%' or ','+@zparam+',' like '%,'+y+',%')
This may have performance issues - take a look at this page if you do: http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.05 seconds. Powered By: Snitz Forums 2000