oracle 计算时间差

1.计算时间差(相隔星期,天数,小时,分钟,秒)

SELECT TO_CHAR(date1,'MMDDYYYY:HH24:MI:SS') date1, TO_CHAR(date2,'MMDDYYYY:HH24:MI:SS') date2,

trunc(86400*(date2-date1))- 60*(trunc((86400*(date2-date1))/60)) seconds, 

trunc((86400*(date2-date1))/60)-60*(trunc(((86400*(date2-date1))/60)/60)) minutes,

trunc(((86400*(date2-date1))/60)/60)-24*(trunc((((86400*(date2-date1))/60)/60)/24)) hours,

trunc((((86400*(date2-date1))/60)/60)/24) days,

trunc(((((86400*(date2-date1))/60)/60)/24)/7) weeks

select sysdate- to_date('2009-07-14 12:00:0' ,'yyyy-mm-dd hh24:mi:ss') from dual;

2.计算小时数

select (sysdate- to_date('2009-07-14 12:00:00','yyyy-mm-dd hh24:mi:ss'))*24 from dual;

3.计算分钟数

select (sysdate- to_date('2009-07-14 12:00:00','yyyy-mm-dd hh24:mi:ss'))*24 *60 from dual;

4.计算秒数

select (sysdate- to_date('2009-07-14 12:00:00','yyyy-mm-dd hh24:mi:ss'))*24 *60*60    from dual;

例:
select to_date('2006-05-11 12:30:01','YYYY-MM-DD HH24:MI:SS')   -to_date('2006-05-11 12:30:00','YYYY-MM-DD HH24:MI:SS') T from dual;
select (to_date('2014-04-01 12:10:00','yyyy-mm-dd hh24:mi:ss')- to_date('2014-04-01 12:10:00','yyyy-mm-dd hh24:mi:ss'))*24 *60 from dual;

5.减一天,一小时,

select sysdate,sysdate-1,sysdate-1/24 from dual;
posted @ 2015-04-23 09:47  songjinduo  阅读(2766)  评论(0编辑  收藏  举报