关于日期

1.计算日期的加减

  select to_date('2010-6-7','yyyy-mm-dd') -5 hd_minus_d,to_date('2010-06-07','yyyy-mm-dd') + 5 hd_add_d,
       add_months(to_date('2010-06-07','yyyy-mm-dd'),-5) hd_minus_m,
       add_months(to_date('2010-06-07','yyyy-mm-dd'),5) hd_add_m,
       add_months(to_date('2010-06-07','yyyy-mm-dd'),-5*12) hd_minus_m,
       add_months(to_date('2010-06-07','yyyy-mm-dd'),5*12) hd_add_m
        from dual 

 

2.计算两个日期的天数

   select to_date('1980-12-07','yyyy-mm-dd') - to_date('1981-02-20','yyyy-mm-dd') from dual

 

3.计算1981-02-20到1980-12-17之间的工作天数

select sum(case when to_char(to_date('1980-12-17','yyyy-mm-dd') + t500.id - 1,'DY') in ('星期六','星期日')

then 0 else 1 end) days
from t500
where t500.id <= to_date('1981-2-20','yyyy-mm-dd') - to_date('1980-12-17','yyyy-mm-dd')+ 1

 

4.计算两个日期之间相差的月数和年数

 select months_between(to_date('2010-06-05','yyyy-mm-dd'),to_date('2010-06-05','yyyy-mm-dd')) from dual

select months_between(to_date('2010-06-05','yyyy-mm-dd'),to_date('2010-06-05','yyyy-mm-dd'))/12 from dual

 

5.计算一年中周内各日期的次数

  with x as (
select  level lv1 from dual
connect by level <= (add_months(trunc(sysdate,'Y'),12)-trunc(sysdate,'Y')
)
)
select to_char(trunc(sysdate,'Y') + lv1-1,'DAY'),count(*) from x
group by to_char(trunc(sysdate,'Y') + lv1-1 ,'DAY')

 

6.确定当前记录与下一条记录相差天数

  select ename,hiredate,next_hd,next_hd - hiredate diff
from (
    select deptno,ename,hiredate,lead(hiredate) over(order by hiredate) next_hd from emp
) where deptno = 10 

阅读更多
文章标签: date 2010 工作
个人分类: ORACle开发
想对作者说点什么? 我来说一句

比较日期先后

a993710932 a993710932

2014-12-01 09:16:44

阅读数:105

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭