I have a sitution where I need to pass a string in order to do an IN
declare @searchString varchar(50)
set @searchString = ' ''Nestle'',''UFI'' '
select * from tbl1 where CompanyName IN (@SearchString)
does’t work.
But if I do:
select * from tbl1 where CompanyName IN ('Nestle','UFI')
It works fine. I cannot understand why one work and the other doesn’t
When you use
INit looks at a set and not a single string expression. Because of thisyou need to implement a function such as
CsvToIntto return a table, like so:And then you can use this function from say within a stored procedure like so:
Where
@Parametercontains a comma seperated string of values like:Nestle, UFI, Test