MONTHS_BETWEEN Function
Description
Returns the number of months between two dates. If timestamp1 is later than timestamp2, the result is positive; otherwise, it is negative.
The difference in the time portion (hours, minutes, seconds) is calculated based on 31 days per month. If the day of timestamp1 and timestamp2 are the same (e.g., both are the 5th), or if both are the last day of their respective months, the time portion is ignored.
Parameters
timestamp1: The first date/timestamp. Supports STRING, DATE, and TIMESTAMP types.timestamp2: The second date/timestamp. Supports STRING, DATE, and TIMESTAMP types.roundOff(optional): BOOLEAN type. When true (default), the result is rounded to 8 decimal places; when false, the full precision is returned.
Returns
DOUBLE type.
Examples
-
Basic usage (default roundOff=true, 8 decimal places):
-
Disable rounding to return full precision:
-
When both dates are month-end, the time portion is ignored:
Notes
- When any parameter is NULL, the result is NULL.
- When the day of both dates is the same, or both are month-end, the time portion is ignored and the result is an integer number of months.
- The time portion difference is converted based on 31 days per month (i.e., 31x24x3600 seconds).
