Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 312
dp3 Avatar asked Jan 09 '13 14:01

dp3


People also ask

How do you use like in parameters?

Create a select query, and then open the query in Design view. In the Criteria row of the field you want to add a parameter to, type Like "*"&[, the text that you want to use as a prompt, and then ]&"*".

What does like %% mean in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

How do you do multiple like conditions in SQL?

Not only LIKE, but you can also use multiple NOT LIKE conditions in SQL. You will get records of matching/non-matching characters with the LIKE – this you can achieve by percentile (%) wildcard character. Below use cases, help you know how to use single and multiple like conditions.


1 Answers

It should be:

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

Instead of:

... WHERE LastName LIKE '%@LastName%' 
like image 121
Mahmoud Gamal Avatar answered Sep 28 '22 03:09

Mahmoud Gamal