日期:2014-05-17  浏览次数:21225 次

查询满足两个时间段的oracle语句
我现在想查出在 '2007-10-09 16:05:04' 时间段 正负5秒的记录 ('2007-10-09 16:05:04' 是个字符串)
select * from kd_mail where senddate between and TO_CHAR(add_seconds(to_date('2007-10-09 16:05:04','yyyy-mm-dd hh24:mi:ss'),5 ) )

我上面的语句 包错 add_seconds 高手帮下,分肯定加
只要实现功能不一定用上面的sql语句

------解决方案--------------------
不知这位XD说的add_seconds函数我在Oracle里怎么没见到过呢 ...

select * 
from kd_mail 
where senddate 
between to_date('2007-10-09 16:05:04 ', 'yyyy-mm-dd hh24:mi:ss') - interval '5' second
and to_date('2007-10-09 16:05:04 ', 'yyyy-mm-dd hh24:mi:ss') + interval '5' second;


------解决方案--------------------
select to_date( '2007-10-09 16:05:04 ', 'yyyy-mm-dd hh24:mi:ss ') +1/24*1/3600*5 from dual;

+1是加一天,1/24*1/3600这个就是加1秒了。
------解决方案--------------------
select to_date('2007-10-09 16:05:04','yyyy-mm-dd hh24:mi:ss')+1 加一天
select to_date('2007-10-09 16:05:04','yyyy-mm-dd hh24:mi:ss')+1/24 加1小时
select to_date('2007-10-09 16:05:04','yyyy-mm-dd hh24:mi:ss')+1/(24*60) 加1分钟
select to_date('2007-10-09 16:05:04','yyyy-mm-dd hh24:mi:ss')+1/(24*60*60) 加1秒钟
类推至毫秒0.001秒
------解决方案--------------------
SQL code
select * from kd_mail where senddate between  
TO_CHAR( to_date('2007-10-11 12:10:10','YYYY-MM-DD HH24:MI:SS')+numtodsinterval(-5,'second'),'YYYY-MM-DD HH24:MI:SS' ) AND 
TO_CHAR( to_date('2007-10-11 12:10:10','YYYY-MM-DD HH24:MI:SS')+numtodsinterval(5,'second'),'YYYY-MM-DD HH24:MI:SS' )