Oracle subtracts two times to get the time difference

1.months_between(date1,date2);

Date1 and date2 are subtracted to get the month difference .

select months_between(to_date('2015-05-11','yyyy-MM-dd'),to_date('2015-04-11','yyyy-MM-dd')) from dual ;相差一个月。

2.ceil(date1-date2);

Subtract date1-date2 to get the difference in 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 ;相差30天。

3. Get the milliseconds difference between the two times 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 number of seconds between the two times 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. Get the minutes difference between the two times 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. Get the hours difference between the two times 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. Other

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 blog.csdn.net/u010919402/article/details/128901555