Sunday, December 22, 2024

MySQL PERIOD_DIFF() Function

 The PERIOD_DIFF() function in MySQL is used to calculate the difference between two periods. A period is represented as an integer in the format YYYYMM, where YYYY is the year and MM is the month.

Syntax:

PERIOD_DIFF(period1, period2)
  • period1: The first period (in YYYYMM format).
  • period2: The second period (in YYYYMM format).

Description:

The PERIOD_DIFF() function returns the difference between the two periods in months. The result is positive if period1 is later than period2, and negative if period1 is earlier than period2.

Example:

SELECT PERIOD_DIFF(202401, 202312);

This will return 1, since January 2024 (202401) is 1 month later than December 2023 (202312).

Another Example:

SELECT PERIOD_DIFF(202305, 202312);

This will return -7, since May 2023 (202305) is 7 months earlier than December 2023 (202312).

No comments:

Post a Comment