从一个个长度为7位的字符串列中查询满足以下条件的记录:第1位 = 2 or * ,第2位 = 3 or * ,第3位 = 8 or * ,第4位 = 5 or * ,第5位 = 9 or * ,第6位 = 7 or * ,第7位 = 4 or * 怎么能用简单方法实现,即不通过一个个字符比较来实现。

解决方案 »

  1.   

    select col from tb where substring(col,1,1) in(1,'*') and substring(col,2,1) in(3,'*') and
    substring(col,3,1) in(8,'*') and substring(col,4,1) in(5,'*') and substring(col,5,1) in(9,'*')
    and substring(col,6,1) in(7,'*') and substring(col,7,1) in(4,'*')
      

  2.   

    declare @t table(col varchar(10))
    insert @t select '2**5974'
    insert @t select '*3**9**'
    insert @t select '2*2597s'
    select col from @t where substring(col,1,1) in(2,'*') and substring(col,2,1) in(3,'*') and
    substring(col,3,1) in(8,'*') and substring(col,4,1) in(5,'*') and substring(col,5,1) in(9,'*')
    and substring(col,6,1) in(7,'*') and substring(col,7,1) in(4,'*')/*
    col        
    ---------- 
    2**5974
    *3**9**
    */
      

  3.   

    declare @t table(col varchar(10))
    insert @t select '2**5974'
    insert @t select '*3**9**'
    insert @t select '2*2597s'
    select col 
    from @t 
    where col like '[2*][3*][8*][5*][9*][7*][4*]'
    /*
    col
    ----------
    2**5974
    *3**9**(2 行受影响)
    */