Oracle MONTHS_BETWEEN function
How to calculate the number of months between two dates in Oracle?
The MONTHS_BETWEEN() function is used to get the number of months between dates (date1, date2). See the following conditions:
- 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.
Uses of Oracle MONTHS_BETWEEN() Function:
- Calculating the number of months between two dates: Determine the difference in months between any two given dates.
- Assessing date intervals for financial calculations: Evaluate monthly periods for interest calculations, payment schedules, or financial reporting.
- Determining age or duration of events: Compute the elapsed time between significant dates such as birthdates or contract dates.
- Handling date-based queries and filters: Use in SQL queries to filter or sort records based on the interval between dates.
- Providing precise date differences: Account for the fractional months based on the exact difference in days between the dates.
Syntax:
MONTHS_BETWEEN(date1, date2)
Parameters:
Name | Description |
---|---|
date1 | The first date |
date2 | The second date. |
Applies to:
Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i
Pictorial Presentation
Example: Oracle MONTHS_BETWEEN () function
The following statement calculates the months between two specified dates:
SQL> SELECT MONTHS_BETWEEN
2 (TO_DATE('02-02-2015','MM-DD-YYYY'),
3 TO_DATE('12-01-2014','MM-DD-YYYY') ) "Months"
4 FROM DUAL;.
Sample Output:
Months ---------- 2.03225806
Previous:
LOCALTIMESTAMP
Next:
NEW_TIME
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/oracle/datetime-functions/oracle-months_between-function.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics