Pass comma separated variable to IN clause in SQL Server

Last Reply 3 months ago By pandeyism

Posted 3 months ago

hi

i want to pass a list of values string like in a dinamic query

DECLARE @Lines AS NVARCHAR(MAX)

SET @Lines = 'PL1,PL2,PL3'

SET @Query = '

WHERE ProductLines IN (@Lines) ...'

How can do that?

Posted 3 months ago

Hi jon,

Refer below link to split value using comma.

Insert Comma Separated (Delimited) values in a Table in SQL Server

Refer below query.

SQL

DECLARE @Lines AS NVARCHAR(MAX)
SET @Lines = 'USA,UK'
SELECT * FROM Customers
WHERE Country IN ( SELECT Item FROM SplitString(@Lines,','))