months_diff
Returns the month difference between two date expressions (expr1
β expr2
), accurate to the month.
Syntaxβ
BIGINT months_diff(DATETIME expr1,DATETIME expr2);
Parametersβ
-
expr1
: the end time. It must be of the DATETIME or DATE type. -
expr2
: the start time. It must be of the DATETIME or DATE type.
Return valueβ
Returns a BIGINT value.
NULL is returned if the date does not exist, for example, 2022-02-29.
Examplesβ
select months_diff('2010-11-30 23:59:59', '2010-1-1 23:59:59');
+---------------------------------------------------------+
| months_diff('2010-11-30 23:59:59', '2010-1-1 23:59:59') |
+---------------------------------------------------------+
| 10 |
+---------------------------------------------------------+
select months_diff('2010-11-30', '2010-1-1');
+---------------------------------------+
| months_diff('2010-11-30', '2010-1-1') |
+---------------------------------------+
| 10 |
+---------------------------------------+