SELECT prod_id, prod_name
FROM products
WHERE prod_name LIKE '_ton anvil%';products表中prod_name列有三行类似选项:.5ton anvil, 1ton anvil, 2ton anvil
想要用这个语句只选出 1ton和2ton两行记录,执行结果为空白记录,请高手指教!多谢!

解决方案 »

  1.   

    WHERE prod_name LIKE '[0-9]ton anvil%'; 
      

  2.   

    你的语句没问题.
    create table tab (prod_id int, prod_name varchar(20))
    insert into tab values(1,'.5ton anvil')
    insert into tab values(2,'1ton anvil') 
    insert into tab values(3,'2ton anvil')
    goselect * from tab where prod_name like '_ton anvil%'drop table tab/*
    prod_id     prod_name            
    ----------- -------------------- 
    2           1ton anvil
    3           2ton anvil(所影响的行数为 2 行)
    */
      

  3.   

    或者你把prod_name 转换为varchar再查询.SELECT prod_id, prod_name 
    FROM products 
    WHERE cast(prod_name as varchar) LIKE '_ton anvil%'; 
      

  4.   

     
    CREATE TABLE TB(N VARCHAR(20))INSERT INTO TB VALUES('.5ton anvil')
    INSERT INTO TB VALUES('1ton anvil')
    INSERT INTO TB VALUES('2ton anvil')SELECT * FROM TB WHERE N LIKE '[1|2]ton anvil%'N
    --------------------
    1ton anvil
    2ton anvil(2 row(s) affected)
      

  5.   

    更正一下,用dawugui的try了一下。没有问题。 
    学习了。向dawugui学习。
      

  6.   


    这个方法还是没有显示记录,如果WHERE prod_name LIKE '%ton anvil%';的话三条记录都能出来,困惑ing
      

  7.   


    这个方法还是没有结果,创建表格时设计prod_name列时属性是“NCHAR(255)”,是直接下载的代码创建的数据库和表格。
      

  8.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb (prod_id int, prod_name varchar(20))
    insert into tb values(1,'.5ton anvil')
    insert into tb values(2,'1ton anvil') 
    insert into tb values(3,'2ton anvil')select * from tb 
    where prod_name like '[0-9]ton anvil%'prod_id prod_name
    2 1ton anvil
    3 2ton anvil怎么可能出不来?
      

  9.   

    引用:
    SELECT prod_id, prod_name 
    FROM products 
    WHERE cast(prod_name as varchar) LIKE '_ton anvil%'; 
    这个是正确的。
      

  10.   


    不行? create表的代码发上来看看!
    dawugui的我也测了。可以用的哦。
      

  11.   

    我在sql server 2000里都试了 上面的方法都可行呀
      

  12.   

    where 字段名 like '_ton anvil%'
      

  13.   

    create table tab (prod_id int, prod_name varchar(20))
    insert into tab values(1,'.5ton anvil')
    insert into tab values(2,'1ton anvil') 
    insert into tab values(3,'2ton anvil')
    go
    select * from tabselect * from tab where prod_name like '_ton anvil%'
    drop table tab
      

  14.   

    LIKE '[1|2]ton anvil%'
      

  15.   

    .5ton anvil, 1ton anvil, 2ton anvil 原来5、1、2和ton之前有个空格,哎,我撞墙去了,耽误大家时间了~