-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
  • 732,580
  • 175
  • 1,330
  • 1,459
Faisal
  • 584
  • 3
  • 11
  • 33

1 Answers1

7

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,242,037
  • 58
  • 646
  • 786