前端之家收集整理的这篇文章主要介绍了
Oracle查询优化日期运算实例详解,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_4040@加减日、月、年
@H
404_0@<span style="color: #0000ff">
在Oracle中,date类型可以直接加减天数,而加减月份要用add_months函数.
<div class="jb51code">
<pre class="brush:
sql;">
select a.hiredate 雇用日期,a.hiredate + 5 加5天,a.hiredate - 5 减5天,add_months(hiredate,5) 加5个月,-5) 减5个月,5
12) 加5年,-5 12) 减5年
from emp a where rownum <=1;
雇用日期 加5天 减5天 加5个月 减5个月 加5年 减5年
1980-12-17 1980-12-22 1980-12-12 1981-05-17 1980-07-17 1985-12-17 1975-12-17
@H_404_9@
sql;">
select a.hiredate,a.hiredate - 5 / 24 / 60 / 60 减5秒,a.hiredate + 5 / 24 / 60 / 60 加5秒,a.hiredate - 5 / 24 / 60 减5分钟,a.hiredate + 5 / 24 / 60 加5分钟,a.hiredate - 5 / 24 减5小时,a.hiredate + 5 / 24 加5小时
from emp a
where rownum <= 1;
@H_
404_9@
select duration,duration * 24 间隔小时,duration * 24 * 60 间隔分钟,duration * 24 * 60 * 60 间隔秒
from (select (max(a.hiredate) - min(a.hiredate)) as duration from emp a where
a.ename in ('ALLEN','WARD')) x;
DURATION 间隔小时 间隔分钟 间隔秒
2 48 2880 172800
@H_404_9@
sql;">
select max_hd - min_hd 间隔天,months_between(max_hd,min_hd) 间隔月,min_hd) /12 间隔年
from (select min(hiredate) min_hd,max(hiredate) max_hd from emp) x;
间隔天 间隔月 间隔年
2348 77.1935483 6.43279569
@H_404_9@
sql;">
with x as
(select level lvl
from dual
connect by level <=
(add_months(trunc(sysdate,'y'),12) - trunc(sysdate,'y')))
select to_char(trunc(sysdate,'y') + lvl - 1,'DAY'),count(*)
from x
group by to_char(trunc(sysdate,'DAY');
@H_
404_9@