MySQL PERIOD_DIFF() Function

In MySQL, the PERIOD_DIFF() function returns the number of months between two periods specified by year and month.

PERIOD_DIFF() Syntax

Here is the syntax of MySQL PERIOD_DIFF() function:

PERIOD_DIFF(period1, period2)

Parameters

period1
Required. Format: YYYYMM or YYMM.
period2
Required. Format: YYYYMM or YYMM.

Return value

The MySQL PERIOD_DIFF() function returns the number of months between two periods specified by year and month.

PERIOD_DIFF() Examples

Here are some examples of the PERIOD_DIFF() function.

SELECT
    PERIOD_DIFF(202202, 202201),
    PERIOD_DIFF(202203, 202201),
    PERIOD_DIFF(202204, 202201),
    PERIOD_DIFF(202205, 202201),
    PERIOD_DIFF(202206, 202201),
    PERIOD_DIFF(202207, 202201),
    PERIOD_DIFF(202208, 202201),
    PERIOD_DIFF(202209, 202201),
    PERIOD_DIFF(202210, 202201),
    PERIOD_DIFF(202211, 202201),
    PERIOD_DIFF(202212, 202201),
    PERIOD_DIFF(202301, 202201)\G
PERIOD_DIFF(202202, 202201): 1
PERIOD_DIFF(202203, 202201): 2
PERIOD_DIFF(202204, 202201): 3
PERIOD_DIFF(202205, 202201): 4
PERIOD_DIFF(202206, 202201): 5
PERIOD_DIFF(202207, 202201): 6
PERIOD_DIFF(202208, 202201): 7
PERIOD_DIFF(202209, 202201): 8
PERIOD_DIFF(202210, 202201): 9
PERIOD_DIFF(202211, 202201): 10
PERIOD_DIFF(202212, 202201): 11
PERIOD_DIFF(202301, 202201): 12