MONTHS_DIFF
Descriptionβ
The MONTHS_DIFF
function calculates the number of complete months between two dates. It accepts two date arguments and returns the difference in months as an integer.
Syntaxβ
MONTHS_DIFF(<enddate>, <startdate>)
Parametersβ
εζ° | θ―΄ζ |
---|---|
<enddate> | The ending date, representing the later date in the difference calculation. Supports DATE (e.g., YYYY-MM-DD ) or DATETIME (e.g., YYYY-MM-DD HH:MM:SS ) types. |
<startdate> | The starting date, representing the earlier date in the difference calculation. Supports DATE (e.g., YYYY-MM-DD ) or DATETIME (e.g., YYYY-MM-DD HH:MM:SS ) types. |
Return Valueβ
returns the number of months resulting from <enddate>
minus <startdate>
- When either
<enddate>
or<startdate>
is NULL, or both are NULL, it returns NULL
Exampleβ
select months_diff('2020-12-25','2020-10-25'),months_diff('2020-10-25 10:00:00','2020-12-25 11:00:00');
+---------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+
| months_diff(cast('2020-12-25' as DATETIMEV2(0)), cast('2020-10-25' as DATETIMEV2(0))) | months_diff(cast('2020-10-25 10:00:00' as DATETIMEV2(0)), cast('2020-12-25 11:00:00' as DATETIMEV2(0))) |
+---------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+
| 2 | -2 |
+---------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+