time difference calculating oracle

1.months_between (date1, date2); date1 date2 and phase difference obtained by subtracting the month.

   select months_between (to_date ( '2015-05-11' , 'yyyy-MM-dd'), to_date ( '2015-04-11', 'yyyy-MM-dd')) from dual; a difference of one month.

2.ceil (date1-date2); date1 -date2 difference obtained by subtracting the number of days

   select ceil (To_date ( '2015-05-11 00:00:00 ', 'yyyy-mm-dd hh24-mi-ss') - to_date ( '2015-04-11 00:00:00', ' yyyy-mm-dd hh24-mi-ss')) from dual; difference of 30 days.

3. Get two time difference of the number of milliseconds ceil ((date1 - DATE2) * 24 * 60 * 60 * 1000)

  SELECT ceil ((To_date ( '2015-05-11 00:00:00', 'YYYY-mm- dd hh24-mi-ss') - To_date ( '2015-04-11 23:59:59', 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60 * 1000) FROM DUAL;

4. Get the difference in time of two seconds ceil ((date1 - date2) * 24 * 60 * 60)
   select ceil((To_date('2015-05-11 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2015-04-11 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 * 60 )  FROM DUAL;

5.获取两时间相差的分钟数 ceil((date1 - date2) * 24 * 60 )

    select ceil((To_date('2015-05-11 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2015-04-11 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60  )  FROM DUAL;

6.获取两时间相差小时数 ceil((date1 - date2) * 24 )

 select ceil((To_date('2015-05-11 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2015-04-11 23:59:59' , 'yyyy-mm-dd hh24-mi-ss')) * 24 )  FROM DUAL;

7.其他

   select sysdate,add_months(sysdate,12) from dual;  --加1年

   select sysdate,TO_CHAR(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual;  --加1天

   select sysdate,TO_CHAR(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual;  --加1小时

   select sysdate,TO_CHAR(sysdate+1/24/60,'yyyy-mm-dd HH23:MI:SS') from dual;  --加1分钟

  select sysdate,TO_CHAR(sysdate+1/24/60/60,'yyyy-mm-dd HH23:MI:SS') from dual;  --加1秒

Guess you like

Origin www.cnblogs.com/hellohero55/p/12000301.html