[Oracle] [21] calculates the number of days between two dates, months, and years

text:

(1) Number of heaven

Direct subtraction of two dates

- the Oracle the number of days between two dates - 
SELECT the TO_NUMBER (the TO_DATE ( ' 2018-6-5 ' , ' YYYY-mm-dd HH24: mi The: SS ' ) - the TO_DATE ( ' 2018-5-31 ' , ' YYYY dd HH24--mm: mi The: SS ' )) from Dual;
 - . 5

(2) the number of months

Function to use months_between

- the Oracle two dates differ by the number of months - 
- 1) is the last day of the month, A date> B date, returns the integer --- 
SELECT MONTHS_BETWEEN (the TO_DATE ( ' 2018-6-30 ' , ' YYYY-mm HH24 -dd: mi The: SS ' ), the TO_DATE ( ' 2018-5-31 ' , ' YYYY-mm-dd HH24: mi The: SS ' ))   from Dual;
 - . 1 

- 2) is the last day of the month, date B> A date, returns negative --- 
SELECT MONTHS_BETWEEN (the TO_DATE ( ' 2018-4-30 ' , ' YYYY-mm-dd HH24: mi The: SS ' ), the TO_DATE ( ' 2018-5-31 ' ,' YYYY-mm-dd HH24: mi The: SS ' )) from Dual;
 - -1 

- . 3) is not the same days of the month, A date> B date, returns the number of decimals --- 
SELECT MONTHS_BETWEEN (the TO_DATE ( ' 2018-6-25 ' , ' YYYY-mm-dd HH24: mi The: SS ' ), the TO_DATE ( ' 2018-5-31 ' , ' YYYY-mm-dd HH24: mi The: SS ' )) from Dual;
 - 0.8064516 ...

(3) Other: Number of seconds, several minutes, hours, years

Directly to the multiplexing the above two methods

- the Oracle difference between the number of hours in the two dates - 
SELECT the TO_NUMBER ((the TO_DATE ( ' 2018-6-5 ' , ' YYYY-mm-dd HH24: mi The: SS ' ) - the TO_DATE ( ' 2018-5-31 ' , ' YYYY-mm-dd HH24: mi the: SS ' )) * 24 ) from Dual;
 - to give: 120 

- the Oracle in the number of minutes between two dates - 
SELECT the TO_NUMBER ((the TO_DATE ( ' 2018-6-5 ' , ' YYYY-mm-dd HH24: mi The: SS ' ) - the TO_DATE ( ' 2018-5-31 ', ' YYYY-mm-dd HH24: mi The: SS ' )) * 24 * 60 ) from Dual;
 - to give: 7200 

- the Oracle in seconds between two dates - 
SELECT the TO_NUMBER ((the TO_DATE ( ' 2018-6 -5 ' , ' YYYY-mm-dd HH24: mi The: SS ' ) - the TO_DATE ( ' 2018-5-31 ' , ' YYYY-mm-dd HH24: mi The: SS ' )) * 24 * 60 * 60 ) from Dual;
 - to give: 43200 

-Oracle two year dates differ - 
SELECT ((MONTHS_BETWEEN (the TO_DATE ( ' 2018-5-31 ' , ' YYYY-mm-dd HH24: mi The: SS ' ), the TO_DATE ( ' 2016-5-31 ' , ' YYYY dd HH24--mm: mi The: SS ' ))) / 12 is ) from Dual;
 - to give: 2

Reference blog:

the number of the oracle two dates differ by days, months, years, hours, minutes, seconds, etc. - Small Xiaoxie - blog Park
https://www.cnblogs.com/xielong/p/9122254.html

Guess you like

Origin www.cnblogs.com/huashengweilong/p/10991791.html