根据两日期字符串到数据库中取值.select * from tbl
where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
and date1 <= to_date('2008-12-22', 'yyyy-mm-dd')这样为什么取不到今天的记录?求教.

解决方案 »

  1.   

    to_date('2008-12-22', 'yyyy-mm-dd')
    出来的结果是2008-12-22 00:00:00
    两个时间段都是2008-12-22 00:00:00
    自然没结果喽
    记住ORACLE里的DATE必定是有时分秒的
    只是00:00:00在工具里不显示出来而已
    select * from tbl
    where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
    and date1 <= to_date('2008-12-22', 'yyyy-mm-dd')+0.99999
    这样才行
      

  2.   

    where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
    and date1 <= to_date('2008-12-22', 'yyyy-mm-dd')to_date('2008-12-22', 'yyyy-mm-dd')取到的是12:22 00:00:00,如果这样取,是00:00:00的,
    如果这个时间没有数据,就不会有
      

  3.   

    to_date('2008-12-22', 'yyyy-mm-dd'),只取年,月,日 会忽略掉了时,分,秒,默认为00:00:00
      

  4.   

    这样写可以:
    select * from tbl
    where TRUNC(date1) >= to_date('2008-12-22', 'yyyy-mm-dd')
    and TRUNC(date1) <= to_date('2008-12-22', 'yyyy-mm-dd')
      

  5.   


    楼主为什么不直接用:
    select * from tbl
    where to_char(date1,'yyyy-mm-dd') = '2008-12-22';
      

  6.   

    如果在列date1上建了索引,这样写就不会用到索引了。
      

  7.   

    只要对字段作了操作,应该都用不到索引,你可以这样:
    where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
    and date1 < to_date('2008-12-22', 'yyyy-mm-dd') + 1 
      

  8.   

    楼上正解。
    where date1 >= to_date('2008-12-22', 'yyyy-mm-dd') and date1 < to_date('2008-12-22', 'yyyy-mm-dd') + 1 
    如果date1字段建立了索引,上面的语句是可以用到索引的。
    我前几天试了一下,如果对一个建了索引的字段同时用到了">=、<="索引就失效了,但是如果只有">=、<"索引就有效。