例如,现在数据库中有一个字段是字符串型的,其中有字符,还有如‘2004-10-10 10:11:11’型的字符,我想查询该字段中的类似时间型的信息。
请问怎样查?
我如下方式不可以:
    select * from TESTtable where CAST(test as datetime) between '2004-1-1 00:00:00' and '2005-1-1 00:00:00'其中 TESTtable是表名,test是欲查的字段。
提示转换错误,但该字段无字符时查询正常,能够得到查询信息。
请问如何解决?多谢,请赐教!

解决方案 »

  1.   

    你用的是什么数据库?? 如果是oracle的话,可以用oracle的日期转换函数:to_date
      

  2.   

    select * from 
    (
    select '2004-10-10 10:11:1' as test 
    union all select '2004-10-11 10:11:1' as test 
    union all select '2004-10-12 10:11:1' as test 
    union all select '2004-10-13 10:11:1' as test 
    union all select '2004-10-14 10:11:1' as test 
    union all select '2004-10-15 10:11:1' as test 
    union all select '2004-10-16 10:11:1' as test 
    union all select '2004-10-17 10:11:1' as test 
    union all select '2004-10-18 10:11:1' as test 
    ) a
    where Convert(DateTime,REPLACE(test,':',':')) > '2004-10-15 10:11:1'
      

  3.   

    结果:
    (所影响的行数为 3 行)
    test
    2004-10-16 10:11:1
    2004-10-17 10:11:1
    2004-10-18 10:11:1
      

  4.   

    这个库干嘛这么设计,好好的日期型数据为何弄成字符型的?还合在一起,转成日期型再弄。
    楼上的方法有点麻烦,用了那么多SELECT,有点浪费系统资源,要是几百万分记录还不得查上几分钟啊