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

前端 未结 6 1306
说谎
说谎 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:18

    Not sure if this would be faster or slower than DavidG's suggestion, but in order to get the same matches with only one check, you can do:

    DECLARE @categoryId INT
    SET @categoryId = 3
    
    SELECT *
    FROM myTable
    WHERE CHARINDEX(',' + CAST(@categoryId AS VARCHAR(MAX)) + ',', ',' + categoryIds + ',') > 0
    

提交回复
热议问题