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

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

    SELECT * 
    FROM myTable 
    WHERE (',' + RTRIM(categoryIds) + ',') LIKE '%,' + @stringId + ',%'
    

    Here @stringId is your text to be searched. In this way you can avoid unnecessary multiple where conditions

    Kind Regards, Raghu.M.

提交回复
热议问题