根据两日期字符串到数据库中取值.select * from tbl
where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
and date1 <= to_date('2008-12-22', 'yyyy-mm-dd')这样为什么取不到今天的记录?求教.
where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
and date1 <= 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
这样才行
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的,
如果这个时间没有数据,就不会有
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')
楼主为什么不直接用:
select * from tbl
where to_char(date1,'yyyy-mm-dd') = '2008-12-22';
where date1 >= to_date('2008-12-22', 'yyyy-mm-dd')
and date1 < to_date('2008-12-22', 'yyyy-mm-dd') + 1
where date1 >= to_date('2008-12-22', 'yyyy-mm-dd') and date1 < to_date('2008-12-22', 'yyyy-mm-dd') + 1
如果date1字段建立了索引,上面的语句是可以用到索引的。
我前几天试了一下,如果对一个建了索引的字段同时用到了">=、<="索引就失效了,但是如果只有">=、<"索引就有效。