SQL Server using wildcard within IN

前端 未结 13 661
终归单人心
终归单人心 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
    SELECT c.* FROM(
    SELECT '071235' AS token UNION ALL SELECT '07113' 
     UNION ALL SELECT '071343'
    UNION ALL SELECT '0713SA'
    UNION ALL SELECT '071443') AS c
    JOIN (
    SELECT '0712%' AS pattern UNION ALL SELECT '0711%' 
     UNION ALL SELECT '071343') AS d
    ON c.token LIKE d.pattern
    
    071235
    07113
    071343
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-02 20:33

    How about:

    WHERE LEFT(job_no, 4) IN ('0711', '0712', ...)
    
    0 讨论(0)
  • 2020-12-02 20:33

    I think I have a solution to what the originator of this inquiry wanted in simple form. It works for me and actually it is the reason I came on here to begin with. I believe just using parentheses around the column like '%text%' in combination with ORs will do it.

    select * from tableName
    where (sameColumnName like '%findThis%' or sameColumnName like '%andThis%' or 
    sameColumnName like '%thisToo%' or sameColumnName like '%andOneMore%') 
    
    0 讨论(0)
  • 2020-12-02 20:36

    You could try something like this:

    select *
    from jobdetails
    where job_no like '071[12]%'
    

    Not exactly what you're asking, but it has the same effect, and is flexible in other ways too :)

    0 讨论(0)
  • 2020-12-02 20:38

    In Access SQL, I would use this. I'd imagine that SQLserver has the same syntax.

    select * from jobdetails where job_no like "0711*" or job_no like "0712*"

    0 讨论(0)
提交回复
热议问题