question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: How to use a parameter in a SQL "like" statement?

See original GitHub 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, 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

7reactions
kristofferostlundcommented, Jan 11, 2018

The query should be something like the following:

// I highly recommend using backticks (`), or at least doble quoation marks for the query strings (")
const query = `SELECT * FROM [dbo].[Users] WHERE [Name] LIKE '%' + @nameSuffix`

// or similar as to how m-adil writes his
const otherQuery = 'SELECT * FROM [dbo].[Users] WHERE [Name] LIKE \'%\' + @nameSuffix + \'%\''

Do note that the query variable should be a single javascript string.

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.


Here’s a full example for selecting all users with a name that ends on son (Olsson, Anderson, etc):

const request = new sql.Request()

request.input('nameSuffix', sql.VarChar, 'son')

const query = `SELECT * FROM [dbo].[Users] WHERE [Name] LIKE '%' + @nameSuffix`

request.query(query)
  .then(value => console.log(value))
  .catch(error => console.log(error))
1reaction
acentfriocommented, Jan 11, 2018

We need working example. Can’t figure out how to make this work =/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found