oracle 查询语句,时间范围

供稿:hz-xin.com     日期:2024-05-04
oracle根据选择的时间范围查询相应的数据

select * from TableA where to_char(startDate,'YYYY-MM-DD')>='2011-09-01' and to_char(endDate,'YYYY-MM-DD')<='2011-09-012' ;
另外如果是程序里面用可以做绑定参数,免得每次都解析SQL语句,可以减少查询时间

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')
如果要查询开区间的数据只需将>= 和和<就行。

sql = "select * from table1 where time >= TO_DATE('" + d1 + "','yyyy-MM-dd-hh-mi-ss') and time <= TO_DATE('" + d2 + "','yyyy-MM-dd-hh-mi-ss')";

--time字段的格式确认是yyyy-MM-dd-hh-mi-ss这种吗?

1、如果time是字符串'yyyy-MM-dd-hh-mm-ss'格式,di、d2采用字符'yyyy-MM-dd-hh-mm-ss'格式。语句可写成如下:
sql:='select * from table1 where time between d1 and d2';
2、如果time是日期型则应该是'2012-9-8 18:55:00' 或 '08-9月 -12 08.50.45' 格式,di、d2采用字符'yyyy-MM-dd-hh-mm-ss'格式。语句可写成如下:
sql:='select * from table1 where to_char(time,'||chr(39)||'yyyy-mm-dd-hh-mm-ss'||
chr(39)||') between d1 and d2';