-2

I'm using a stored procedure and I pass it a string with comma like this:

Apple,Banana,Orange

I want to use this string inside an IN clause, but I don't want to use

SELECT value 
FROM STRING_SPLIT(@MyStr, ',')

How can I generate string that I can use like

SELECT * 
FROM MyTable 
WHERE Fruit IN (@MyStr)
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Faisal
  • 524
  • 1
  • 10
  • 28

1 Answers1

6

Just use a subquery:

select *
from mytable
where fruit in (select value from STRING_SPLIT(@MyStr, ','));

In older versions of SQL Server, you can use like:

select *
from mytable
where ',' + @MyStr + ',' like '%,' + fruit + ',%';
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709