개인적인 정리

[Oracle] MONTHS_BETWEEN() 본문

DB/ORACLE

[Oracle] MONTHS_BETWEEN()

yeon.Biju 2020. 4. 10. 10:24

오라클 MONTHS_BETWEEN()

 

MONTHS_BETWEEN()

   -

 

MONTHS_BETWEEN(date1, date2)

의 형태

 

MONTHS_BETWEEN returns number of months between dates date1 and date2. The month and the last day of the month are defined by the parameter NLS_CAENDAR. 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 day 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

 

 

SQL>

SELECT
    MONTHS_BETWEEN(SYSDATE, TO_DATE('2020-04-01', 'YYYY-MM-DD')) 
FROM DUAL;

   --> 0.304336170848268

 

SQL>

SELECT
    MONTHS_BETWEEN(TO_DATE('2020-01-01', 'YYYY-MM-DD'), TO_DATE('2020-04-01', 'YYYY-MM-DD')) 
FROM DUAL;

--> -3

 

SQL>

SELECT
    MONTHS_BETWEEN(SYSDATE, ADD_MONTHS(SYSDATE, 6)) 
FROM DUAL;

---> -6

 

SQL>

SELECT 
    MONTHS_BETWEEN(TO_DATE('2020-04-01', 'YYYY-MM-DD'), TO_DATE('2020-03-31', 'YYYY-MM-DD'))  
FROM DUAL;

--> 0.032258064516129

 

 

* ORA-01821: date format not recognized

TO_DATE('2020-04-01', 'YYYY-MM-DD')과 같이 사용해야 하는데

TO_DATE('YYYY-MM-DD', '2020-04-01')와 같이 사용하면 발생한다. 

 

'DB > ORACLE' 카테고리의 다른 글

[Oracle] NTH_VALUE()  (0) 2020.04.10
[Oracle] NEXT_DAY()  (0) 2020.04.10
[Oracle] NCHR()  (0) 2020.04.10
[Oracle] NANVL()  (0) 2020.04.10
[Oracle] MOD()  (0) 2020.04.09
[Oracle] MIN()  (0) 2020.04.09
[Oracle] MEDIAN()  (0) 2020.04.09
[Oracle] MAX()  (0) 2020.04.09
Comments