PERIOD_DIFF

PERIOD_DIFF is an SQL function used to compute the difference between two periods. The periods are given in the format of YYYYMM or YYMM. The function returns an integer denoting the number of months between the two periods.

PERIOD_DIFF(P1, P2)

  • p1: The earlier period in the form of YYMM or YYYYMM to compare.
  • p2: The later period in the form of YYMM or YYYYMM to compare.

Example

SELECT PERIOD_DIFF(202012,201911) as Months_Difference;

Output

+-------------------+
| Months_Difference |
+-------------------+
| 13 |
+-------------------+

Explanation

The PERIOD_DIFF function in MySQL calculates the difference in months between two periods. In the provided example, the calculation is between the periods of December 2020 (202012) and November 2019 (201911), and it yields an output of 13 months difference.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.