0

I have below test data:

I have a variable @var int = '5' and I want to see if @var exists in one of the 'data' row in the table, I should report pid,true. So is it possible to do

case when @var in (data....) ? That is can I find @var using 'in' condition in the table data column?

Any help?!

Ilyes
  • 14,640
  • 4
  • 29
  • 55
Rick
  • 1,392
  • 1
  • 21
  • 52
  • Comma delimited data in a single field is a bad idea. Fix your schema and this becomes as simple as `SELECT pid FROM your_fixed_table where data = 5;` – JNevill Oct 19 '18 at 20:56
  • Agree, poor design, but if you're stuck charindex() would help, you would just need to convert @var. Something like charindex(convert(varchar,@var),data,1). A value greater than 0 means it exist. – Tim Mylott Oct 19 '18 at 21:33
  • Another solution would be to implement a string-splitter table-valued function (there are several here). Then you could go `Where @var in (select element from string_split(data, ','))`. – Brian Oct 19 '18 at 21:43

0 Answers0