现在有一个code字段
abc0001200001
abc0001200101
abc0001200201
abc0001200301
abc0001200401
:
:
abc9999200099
abc9999200199
abc9999200299
abc9999200399
abc9999200499
其中的每种植都有从2000年到2004年的记录,数据量比较大。
我想把abcXXXXX2000XX,abcXXXXX2001XX,abcXXXXX2002XX,abcXXXXX2003XX,
abcXXXXX2004XX的记录分别筛选出来。
用的SQL是:
select * from table where code like 'abc%%%%2000%%';
select * from table where code like 'abc%%%%2001%%';
select * from table where code like 'abc%%%%2002%%';
select * from table where code like 'abc%%%%2003%%';
select * from table where code like 'abc%%%%2004%%';
这样的SQL总是筛不出我想要的正确的记录
如在筛选2000年的时候abc2000200123这样的记录也出现了;
后来改用select * from table where code like 'abc????2000??'
这样一条记录也筛选不出来了。
请问高手该如何写SQL命令,多谢!
 

解决方案 »

  1.   

    用字符串啊
    where substr(code,8,4)='2001' or substr(code,8,4)='2002' or substr(code,8,4)='2003' or substr(code,8,4)='2004'
      

  2.   

    上面的不好,太长了,这样吧
    where substr(code,8,4) in ('2001','2002','2003','2004')orcale中可以的
    sql server ,你查一下联机帮助,找字符串函数
      

  3.   

    select * from table where code like 'abc____2000__';
      

  4.   

    select * from table where code like 'abc^^^^2000^^';
    select * from table where code like 'abc^^^^2001^^';
    select * from table where code like 'abc^^^^2002^^';
    select * from table where code like 'abc^^^^2003^^';
    select * from table where code like 'abc^^^^2004^^';在微软的数据库或ORACE上能实现,别的数据库不敢保证啊
      

  5.   

    select * from 表 where  cast(substring(code,8,4) as int)<=2004 and cast(substring(code,8,4) as int)>=2001 and substring(code,1,3)='abc'
      

  6.   

    多谢weizi2000(秋风啊)!
    问题解决了!也谢其他朋友的支持!