1

I would like assign this text separated comma to IN. Have you got idea how to do it?

DECLARE @TESTTEXT varchar(200)

SET @TESTTEXT = '101,103,104'

SELECT 1 WHERE '101' IN (@TESTTEXT)

enter image description here

Rafał Developer
  • 2,135
  • 9
  • 40
  • 72
  • Articles worth reading are [Split strings the right way – or the next best way](http://sqlperformance.com/2012/07/t-sql-queries/split-strings), [Splitting Strings : Now with less T-SQL](http://sqlperformance.com/2012/08/t-sql-queries/splitting-strings-now-with-less-t-sql) – GarethD Feb 27 '15 at 10:52

2 Answers2

3

use dynamic SQL

DECLARE @TESTTEXT varchar(200)
SET @TESTTEXT = '101,103,104'

exec('SELECT 1 WHERE ''101'' IN ( ' + @TESTTEXT + ')')
juergen d
  • 201,996
  • 37
  • 293
  • 362
2

Use LIKE:

select 1 where ',' || @TESTTEXT || ',' like '%,101,%'

The extra commas are there to handle first and last item in list.

|| is ANSI SQL for concat, perhaps tsql has other notation?

jarlh
  • 42,561
  • 8
  • 45
  • 63