create table #a(id int identity,name varchar(20))
insert into #a values('jb07')
insert into #a values('jb7')
insert into #a values('jb08')
insert into #a values('jb8')
insert into #a values('jb9')
insert into #a values('jb10')
insert into #a values('jba7')
insert into #a values('jb17')我要从数据库中拿出:jb7或jb07,jb17三个记录。。(也就是说,jb是固定的,7前面可能有个0-9的数字,也可能没有。都需要拿出来。)
select * from #a where name like 'jb[-0-1]7'  ----这样拿不出jb7  。。当然用or 可以拿出来。
我想问的是能否在like后面通过表达式拿出这种格式的数据。

解决方案 »

  1.   

    like 'jb[0-1]7'
    意思是jb和7之间必须是0或1,
    什么也没有是不会出来的
      

  2.   


    create table #a(id int identity,name varchar(20)) 
    insert into #a values('jb07') 
    insert into #a values('jb7') 
    insert into #a values('jb08') 
    insert into #a values('jb8') 
    insert into #a values('jb9') 
    insert into #a values('jb10') 
    insert into #a values('jba7') 
    insert into #a values('jb17') goselect * from #a where name like 'jb[0-9]7' or name like 'jb7'id          name                 
    ----------- -------------------- 
    1           jb07
    2           jb7
    8           jb17(所影响的行数为 3 行)
      

  3.   


    create table #a(id int identity,name varchar(20)) 
    insert into #a values('jb07') 
    insert into #a values('jb7') 
    insert into #a values('jb08') 
    insert into #a values('jb8') 
    insert into #a values('jb9') 
    insert into #a values('jb10') 
    insert into #a values('jba7') 
    insert into #a values('jb17') select * from #a where name like 'jb[0-9]7' or name like 'jb7'drop table #aid          name
    ----------- --------------------
    1           jb07
    2           jb7
    8           jb17(3 行受影响)
      

  4.   

    % 包含零个或更多字符的任意字符串。 WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名。 
    _(下划线) 任何单个字符。 WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。 
    [ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 WHERE au_lname LIKE '[C-P]arsen' 将查找以arsen 结尾且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如,Carsen、Larsen、Karsen 等。 
    [^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。 WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始且其后的字母不为 l 的所有作者的姓氏。 
      

  5.   

    select * from #a where name like 'jb[0-9]7' or name like 'jb7'
      

  6.   

    select * from #a where name like 'jb[0-9]7' union
    select * from #a where name= 'jb7' 用union,哈哈
      

  7.   

    都不看题,BS用OR和UNION ALL的(不区分大小写)
      

  8.   

    select * from #a where name like 'jb[0-9]7' or name like 'jb7'
    [0-9]  中括号数字从0到9之间的数字,不能取空  
    jb07 jb17 jb27 jb37 jb47 jb57 jb67 jb77 jb87 jb97 
      

  9.   


    select * from #a where name like 'jb%7' and name not like 'jb[a-z]7'
      

  10.   

    select * from #a where [name] in (select [name] from #a where [name] like 'jb%7' and [name] not like 'jb[a-z]7')不用or的话`  这样搞吧