oracle 如何查询在一个日期区间的中的数据?

现要做报表,要查出在当天所处的日期区间的某些数据,sql语句该怎么写?
就是说 无法直接输入日期,日期得在其他表的start date 和 end date 中获得。

oracle 查询日期区间内的数据一般最常用的就是between and 和>=,<=(或者不要等号)了;
举例:select * from tablename t where t.日期列 between to_date('2015-10-20 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2015-10-20 23:59:59','yyyy-mm-dd hh24:mi:ss')
或者:
select * from tablename where t.日期列 >= to_date('2015-10-20 00:00:00','yyyy-mm-dd hh24:mi:ss') and t.日期列 <= to_date('2015-10-20 23:59:59','yyyy-mm-dd hh24:mi:ss')
如果要查询开区间的数据只需将>= 和<=改为>和<就行。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-28
Select * From a
Where Time Between To_Date('20120827080000', 'yyyymmddhh24miss') And To_Date('20120828080000', 'yyyymmddhh24miss')

Select * From a
Where start Date>=To_Date('20120827080000', 'yyyymmddhh24miss') And
end Date=<To_Date('20120828080000', 'yyyymmddhh24miss')追问

-,- 看看我的问题补充~谢啦~

追答

Select * From a
Where start Date>=To_Date('20120827080000', 'yyyymmddhh24miss') And
end Date=<To_Date('20120828080000', 'yyyymmddhh24miss')

追问

不是不是 就是你那20120827080000这放的是 start_date ..不能直接写日期

追答

我汗。。。。啥意思啊,start_date 不是个字段吗?里面没数据?

追问

就是说 日期区间 是存在一个日期区间表中的,要先判断 当天处于哪个日期区间,在查询出 该区间中的数据。

追答

这个得有PLSQL了吧
比如
Declare
v_time Data;
Select Sysdate Into v_time From dual;
If v_time>?? Then
...
End

第2个回答  2012-08-28
select *
from a
where a.time between
(select b.startdate
from b b
where sysdate between startdate and enddate)
and (select b.enddate
from b b
where sysdate between startdate and enddate);本回答被提问者采纳
相似回答