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 2012 Forums
 Transact-SQL (2012)
 Like with wildcard not working

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-07-25 : 20:50:56
hi

There is something wrong with my where statement. It returns results even though i specify a so called empty string to the stored proc.

How should i prevent returning result when is empty? Thanks a lot

here is my code:

Create proc TestEmployee_Search
@searchnameemail as nvarchar(50)
as
Set NoCount On
SELECT [FirstName]
,[LastName]
,[Email]
FROM [dbo].[Account]
where '' + ([FirstName] + ' ' + [LastName] + ' ' + [Email]) + '' like '%' + '' + @searchnameemail + '' + '%'

Exec TestEmployee_Search ''

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-07-25 : 21:23:51
[code]LIKE '%' + NULLIF ( @searchnameemail , '') + '%'[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2014-07-25 : 21:34:26
Thanks KH. It works
Go to Top of Page
   

- Advertisement -