Is there a combination of “LIKE” and “IN” in SQL?

后端 未结 25 1665
灰色年华
灰色年华 2020-11-22 03:08

In SQL I (sadly) often have to use \"LIKE\" conditions due to databases that violate nearly every rule of normalization. I can\'t change that right now. But tha

25条回答
  •  后悔当初
    2020-11-22 03:59

    This works for comma separated values

    DECLARE @ARC_CHECKNUM VARCHAR(MAX)
    SET @ARC_CHECKNUM = 'ABC,135,MED,ASFSDFSF,AXX'
    SELECT ' AND (a.arc_checknum LIKE ''%' + REPLACE(@arc_checknum,',','%'' OR a.arc_checknum LIKE ''%') + '%'')''
    

    Evaluates to:

     AND (a.arc_checknum LIKE '%ABC%' OR a.arc_checknum LIKE '%135%' OR a.arc_checknum LIKE '%MED%' OR a.arc_checknum LIKE '%ASFSDFSF%' OR a.arc_checknum LIKE '%AXX%')
    

    If you want it to use indexes, you must omit the first '%' character.

提交回复
热议问题