MONTHS_BETWEEN

Subtracts to find the number of months between two dates.

Syntax

MONTHS_BETWEEN(date1, date2)

Arguments

  • date1, date2: a valid date, datetime, or parsable date string. Only the date part is used.

Return Type

Integer. If date1 or date2 is not a valid date, returns NULL.

Examples

SELECT MONTHS_BETWEEN(DATE('2017-03-01'), DATE('2017-01-01'));
+--------------------------------------------------------+
| MONTHS_BETWEEN(DATE('2017-03-01'), DATE('2017-01-01')) |
+--------------------------------------------------------+
|                                                      2 |
+--------------------------------------------------------+
1 row in set (0.06 sec)

If date1 is in the past, the function returns a negative result.

SELECT MONTHS_BETWEEN(DATE('2017-01-01'), DATE('2017-03-01'));
+--------------------------------------------------------+
| MONTHS_BETWEEN(DATE('2017-01-01'), DATE('2017-03-01')) |
+--------------------------------------------------------+
|                                                     -2 |
+--------------------------------------------------------+
1 row in set (0.06 sec)

NOW() can be used as one of the parameters.

SELECT MONTHS_BETWEEN('2017-10-31',NOW());
+------------------------------------+
| MONTHS_BETWEEN('2017-10-31',now()) |
+------------------------------------+
|                  2.741935483870968 |
+------------------------------------+
1 row in set (0.06 sec)

Related Topics

Last modified: May 31, 2023

Was this article helpful?