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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Skip conditions in where clause besed on parameter

Author  Topic 

ranga.rav
Starting Member

1 Post

Posted - 2013-03-25 : 07:59:43
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-25 : 08:48:03
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-25 : 08:55:09
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
   

- Advertisement -