dp3 dp3 - 2 years ago 106
SQL Question

T-SQL and the WHERE LIKE %Parameter% clause

I was trying to write a statement which uses the WHERE LIKE '%text%' clause, but I am not receiving results when I try to use a parameter for the text. For example, this works:

SELECT Employee WHERE LastName LIKE '%ning%'

This would return users Flenning, Manning, Ningle, etc. But this statement would not:

DECLARE @LastName varchar(max)
SET @LastName = 'ning'
SELECT Employee WHERE LastName LIKE '%@LastName%'

No results found. Any suggestions? Thanks in advance.

Answer Source

It should be:

WHERE LastName LIKE '%' + @LastName + '%';

Instead of:

WHERE LastName LIKE '%@LastName%'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download