Oracle的months_between(date1,date2)函式注意點
select months_between(to_date('2011-4-28','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
select months_between(to_date('2011-4-29','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2.03225806451613
select months_between(to_date('2011-4-30','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
2011-4-30日期大於2011-4-29,按理說第三個結果應該大於第二個結果,但是結果卻是相反的。
後來查閱了oracle的官方文件,是這樣解釋的:(小數部分是按照一個月31天計算的)
MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.
select 1/31 from dual;
1/31
----------
0.03225806
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
select months_between(to_date('2011-4-29','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2.03225806451613
select months_between(to_date('2011-4-30','yyyy-MM-dd'),to_date('2011-2-28','yyyy-MM-dd'))
from dual;
MONTHS_BETWEEN(TO_DATE('2011-4
------------------------------
2
2011-4-30日期大於2011-4-29,按理說第三個結果應該大於第二個結果,但是結果卻是相反的。
後來查閱了oracle的官方文件,是這樣解釋的:(小數部分是按照一個月31天計算的)
MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise Oracle Database calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.
select 1/31 from dual;
1/31
----------
0.03225806
這樣就能解釋了。
相關文章
- 通過JavaScript定義函式的注意點JavaScript函式
- Oracle ddl隱式提交注意點Oracle
- 自定義函式索引使用及其注意點函式索引
- python裝飾器管理函式和類的注意點Python函式
- GO 的匿名函式使用注意Go函式
- 函式注意事項函式
- javascript關於Array()建構函式引數注意點JavaScript函式
- spring呼叫帶引數的oracle函式應注意的問題SpringOracle函式
- 【函式】ORACLE函式大全函式Oracle
- setbuf函式使用注意事項函式
- C#的Replace函式,使用函式作為的引數需要注意C#函式
- 【函式】oracle視窗函式over()的理解函式Oracle
- Oracle的dump函式Oracle函式
- Oracle的日期函式Oracle函式
- Oracle常用的函式Oracle函式
- Oracle聚合函式/分析函式Oracle函式
- 使用find_in_set()函式的注意事項函式
- 【函式】Oracle EXTRACT()函式與to_char() 函式函式Oracle
- oracle 函式Oracle函式
- oracle or 函式Oracle函式
- Oracle函式Oracle函式
- oracle的分析函式over 及開窗函式Oracle函式
- 【函式】Oracle函式系列(2)--數學函式及日期函式函式Oracle
- Linux上安裝oracle的注意點LinuxOracle
- 【函式】oracle nvl2 函式函式Oracle
- 使用箭頭函式的幾個注意事項函式
- 關於function建構函式特別注意的Function函式
- 【Oracle的NVL函式用法】Oracle函式
- Oracle dump函式的用法Oracle函式
- Oracle trunc()函式的用法Oracle函式
- Oracle 函式 Translate 的用法Oracle函式
- Oracle函式的確定性Oracle函式
- oracle的with函式用法示例Oracle函式
- Oracle 分析函式的使用Oracle函式
- Oracle的日期函式(轉)Oracle函式
- Oracle10g 使用注意點Oracle
- JAVA中函式的特點Java函式
- Oracle 函式大全(字串函式,數學函式,日期函式,邏輯運算函式,其他函式)Oracle函式字串