T-SQL and the WHERE LIKE %Parameter% clause

sql like with parameter c#
sql contains vs like
sql like wildcard
multiple like in sql
sql server like
how to pass parameter in like operator in sql server
oracle sql contains
sql query builder like parameter

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.

It should be:

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

Instead of:

...
WHERE LastName LIKE '%@LastName%'

Question: How to use a parameter in a SQL "like" statement? · Issue , How to use a parameter in a SQL "like" statement? I think I am losing it but I can't figure this out. request.input('param1', sql.VarChar  The SQL LIKE Operator. 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 represents a single character.

The correct answer is, that, because the '%'-sign is part of your search expression, it should be part of your VALUE, so whereever you SET @LastName (be it from a programming language or from TSQL) you should set it to '%' + [userinput] + '%'

or, in your example:

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

SQL LIKE Operator, The SQL LIKE Operator. 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  Basically, in plain T-SQL code, the LIKE statement is a SQL-string, but since we're using a variable we'll have to concat the initial dollar sign ('%') with the variable @nameSuffix. This is true also for our T-SQL in JavaScript code as well, but we'll have to be cautious with how we declare the JavaScript string.

you may try this one, used CONCAT

WHERE LastName LIKE Concat('%',@LastName,'%')

LIKE (Transact-SQL), Pattern Matching with the ESCAPE Clause. You can search for character strings that include one or more of the special wildcard characters. For  Howto? Parameters and LIKE statement SQL. Ask Question Asked 11 years, 5 months ago. Active 2 years, 8 months ago. T-SQL and the WHERE LIKE %Parameter% clause. 2.

LIKE Operator with Parameter – SQLServerCentral, If you really must build the statement using a variable, then you will have to use dynamic sql: DECLARE @Column VARCHAR(20), @VALUE  T-SQL and the WHERE LIKE %Parameter% clause. Ask Question Asked 7 years, 3 months ago. Active 1 year, 6 months ago. Viewed 118k times 74. 6. I was trying to write a

Examples and Function for Using SQL Server LIKE , Using a Parameter to Store a Value for LIKE in T-SQL we can use the ESCAPE clause with the LIKE operator to tell the query engine to use  When you use Unicode data (nchar or nvarchar data types) with LIKE, trailing blanks are significant; however, for non-Unicode data, trailing blanks aren't significant. Unicode LIKE is compatible with the ISO standard. ASCII LIKE is compatible with earlier versions of SQL Server.

SQL Server: LIKE Condition, The SQL Server (Transact-SQL) LIKE condition allows wildcards to be used. The SQL Server (Transact-SQL) LIKE condition allows wildcards to be used in the WHERE clause of a SELECT, INSERT, UPDATE, Parameters or Arguments​. To enable nullable parameters with and AND condition just use IsNull or Coalesce for comparison, which is not needed in your example using OR. e.g. if you would like to compare for Location AND Date and Time. @location nchar(20), @time time, @date date as select DonationsTruck.VechileId, Phone, Location, [Date], [Time] from Vechile

Comments
  • thanks for the earlier tip on the question. But it wasn't though. Anyway in the quest of a high performing answer - is this useful or not? :)
  • @bonCodigo I don't know really, performance and optimization is not my area. Furthermore, these functions are vendor specific, in your case it depends on how the Oracle RDBMS evaluate them, and I don't know Oracle. Sorry.
  • This didn't work for me. The % needs to be in the addParameter section. See James Curran answer here stackoverflow.com/questions/251276/…
  • see my answer (currently below). the wildcard-symbol is PART of the SEARCH expression, not part of the sql-query. It is entered by the USER (or, if wildcard-search is predefined, is appended to the users search expression input). If you append it via string concatanation on database-level, you get an unreusable query-string