SQL Server using wildcard within IN

前端 未结 13 782
终归单人心
终归单人心 2020-12-02 20:07

Since I believe this should be a basic question I know this question has probably been asked, but I am unable to find it. I\'m probably about to earn my Peer Pressure badge,

13条回答
  •  独厮守ぢ
    2020-12-02 20:31

    I had a similar goal - and came to this solution:

    select *
    from jobdetails as JD
    where not exists ( select code from table_of_codes as TC 
                          where JD.job_no like TC.code ) 
    

    I'm assuming that your various codes ('0711%', '0712%', etc), including the %, are stored in a table, which I'm calling *table_of_codes*, with field code.

    If the % is not stored in the table of codes, just concatenate the '%'. For example:

    select *
    from jobdetails as JD
    where not exists ( select code from table_of_codes as TC 
                          where JD.job_no like concat(TC.code, '%') ) 
    

    The concat() function may vary depending on the particular database, as far as I know.

    I hope that it helps. I adapted it from:

    http://us.generation-nt.com/answer/subquery-wildcards-help-199505721.html

提交回复
热议问题