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 2008 Forums
 Transact-SQL (2008)
 dynamic where..

Author  Topic 

arielspecial
Starting Member

11 Posts

Posted - 2011-10-03 : 04:04:44
I couldn't find the right question to find this:
I have an sp which gets params: a few of the params are search terms like: @email, @fromDate, @toDate. I want to check if, for example @email is not empty, and if not add to the 'where' part the query:
where @email like '%aaa@aaa.net%' and if its empty do nothing.
I tried using IF (len(@email)=0) then ... but it didn't really work. is this the right way or is there another better approach?
(also tried using exec(sql_query) but with complex queries I had many errors and it seemed unreadable).

arielspecial
Starting Member

11 Posts

Posted - 2011-10-03 : 04:24:59
I realized I never tried searching for : '"sql server" "dynamic where"' and I found this:
http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=469

use pubs

declare @LastName as nvarchar(100)

select @LastName = 'White'

select au_id,
au_fname,
au_lname
from Authors
where au_lname = case
when DataLength(@LastName) > 0 then @LastName
else au_lname
end


but still there is a problem here with au_lname like '%@LastName%' or is it not a problem?
Go to Top of Page

vmvadivel
Yak Posting Veteran

69 Posts

Posted - 2011-10-03 : 04:31:56
Make use of COALESCE. Something like this:

WHERE
EmailID LIKE '%' + COALESCE(@email, EmailID) + '%'

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

arielspecial
Starting Member

11 Posts

Posted - 2011-10-03 : 05:48:51
OK, I create a query string and concatenate what I need using 'if' and than I exec(@queryStr) and its working fine.

declare @queryStr nvarchar(max)
declare @email nvarchar(50)
set @queryStr = 'select * from tbl where pid=1 '
if (DataLength(@email)>0) select @queryStr = @queryStr + ' and email like '%' + @email '%'' '

exec(@queryStr)

Thanks
Go to Top of Page

vmvadivel
Yak Posting Veteran

69 Posts

Posted - 2011-10-03 : 08:23:34
You don't need a dynamic query for this requirement. You can avoid it by using the sample shown in my previous post.

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

arielspecial
Starting Member

11 Posts

Posted - 2011-10-03 : 08:51:06
Vadivel, thank you for your reply.
as I looked into COALESCE, I read that:

COALESCE(expression1,...n) is equivalent to the following CASE expression:
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
WHEN (expression2 IS NOT NULL) THEN expression2
...
ELSE expressionN
END

My problem is not to check if @email is null or empty, and than do something, but to actually control the format of the Where clause, like: if @email is empty do x, if it has digits in it do Y, and if not do Z in the Where clause.
I don't know, it's not clear enough.
Go to Top of Page

arielspecial
Starting Member

11 Posts

Posted - 2011-10-03 : 09:07:49
also found this:
Dynamic Search Conditions in T-SQL
http://www.sommarskog.se/dyn-search-2008.html
Go to Top of Page
   

- Advertisement -