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.

Is there an example on how to use the WHERE field IN clause?

See original GitHub issue

Hi Patrik,

I’m trying to perform a simple select query as below:

select * from user_table where id in (@id)

I specify the id parameter as a string ‘1,2,3’, but somehow SQL server fails to Convert this from nvarchar to int. I am relying on .input() function’s capability of assigning a type, when it is unspecified.

However, when I do a query from SQL Server, the query select * from user_table where id in (1,2,3) works well.

An example on how to use the WHERE field IN clause would be very useful.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
Shockolatecommented, Jul 12, 2017

@rohithshenoyg , you could do a reduce on your array to construct the SQL string. Something like:

const array = [1, 2, 3]
const sqlString = `SELECT * FROM user_table WHERE id IN (${array.reduce((prev, curr) => {
  return `${prev},${curr}`;
})})`;
request.query(sqlString);
4reactions
jcollumcommented, Jan 26, 2017

Array.join is much simpler there:

$ node --harmony
> const array = [1, 2, 3]
undefined
> const str0 = `SELECT * FROM user_table WHERE id IN (${array.join(',')})`
undefined
> str0
'SELECT * FROM user_table WHERE id IN (1,2,3)'
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use WHERE Clauses in SQL - DigitalOcean
It will return the name and wins values from any row whose wins column value is equal to 1 : SELECT name, wins....
Read more >
How to Write a WHERE Clause in SQL - LearnSQL.com
This article covers how to use the SQL WHERE clause in detail, with practical examples using sample data sets.
Read more >
SQL WHERE IN | NOT IN - Dofactory
WHERE IN returns values that match values in a list. This list is either hardcoded or generated by a subquery. WHERE IN is...
Read more >
SQL WHERE Clause - W3Schools
The SQL WHERE Clause​​ The WHERE clause is used to filter records. It is used to extract only those records that fulfill a...
Read more >
SQL: WHERE Clause - TechOnTheNet
This SQL tutorial explains how to use the SQL WHERE clause with syntax and examples. ... and apply conditions in a SELECT, INSERT,...
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