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
 Filter row based on different non null values

Author  Topic 

anujpratap84
Starting Member

45 Posts

Posted - 2011-07-27 : 06:44:11
Hi,

I have a table with 5 columns.
I want to search data from the table based on passed 5 parameter values to stored procedure.

if value of any parameter is null then do not apply filter on that column only non null values will be in where clause.

Like
@param1 = 1 @param=2 @param3=3 @param4 = null @param5 = null
then only 1,2 and 3 parameter will be in where clause.

Thanks in advance

Anuj Pratap Singh

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-27 : 06:57:04
where
(col1 = @param1 or @param1 is null)
and
(col2 = @param2 or @param2 is null)
...



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-07-27 : 06:58:53
Where (@Param1 is not null and Col1 = @Param1)
or (@Param2 is not null and Col2 = @Param2)
...

Corey

I Has Returned!!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-27 : 07:06:33
But which is better? Corey's or Freds? Or would both have the same performance? http://www.sommarskog.se/dyn-search-2005.html
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-07-27 : 08:26:21
Actually, they would have different results... but that article is interesting. Poorly laid out, but interesting



Corey

I Has Returned!!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-27 : 08:42:19
quote:
Originally posted by Seventhnight
Actually, they would have different results...


I thought that


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -