How can I check whether a number is contained in comma separated list stored in a varchar column?

前端 未结 6 1352
说谎
说谎 2021-01-11 12:48

I have a table with a varchar column categoryIds. It contains some IDs separated by commas, for example:

id       categoryIds
-----         


        
6条回答
  •  滥情空心
    2021-01-11 13:11

    You could use dynamic SQL like this:

    DECLARE     @categoryIds    nvarchar(50) = '1, 2, 3, 4, 5'
    
    EXEC        ('SELECT      *
                  FROM        myTable
                  WHERE       categoryId IN (' + @categoryIds + ')')
    

提交回复
热议问题