Question: How to use a parameter in a SQL "like" statement?
See original GitHub issueHow 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, req.body.search);
var query = "SELECT * FROM table WHERE name LIKE '%@param1'";
Query statement above will not evaluate the @param1 when run.
Ideas?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5
Top Results From Across the Web
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...
Read more >SQL LIKE Operator - W3Schools
CustomerID CustomerName ContactName Address
1 Alfreds Futterkiste Maria Anders Obere Str. 57
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución...
3 Antonio...
Read more >Parameterized Query using an SQL like clause - MSDN
User1804063303 posted. I want to query an SQL Server table using the LIKE clause. Here's the code I use. Dim myString as String...
Read more >SQL: LIKE Condition - TechOnTheNet
The LIKE condition is used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement. If playback doesn't begin shortly, try...
Read more >SQL LIKE Wildcard with Underscore and More Examples
Using a Parameter to Store a Value for LIKE in T-SQL ... To get around this, we can use the ESCAPE clause with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The query should be something like the following:
Do note that the
queryvariable should be a single javascript string.Basically, in plain T-SQL code, the
LIKEstatement 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.Here’s a full example for selecting all users with a name that ends on
son(Olsson, Anderson, etc):We need working example. Can’t figure out how to make this work =/