Open In App

PERIOD_DIFF() Function in MySQL

PERIOD_DIFF() function :
This function in MySQL is used to return the difference between two specified periods. Here the returned results will be in months (either negative or positive) and two periods parameters used in this function should be in the same format.

Syntax :



PERIOD_DIFF(period1, period2)

Parameter :
This method accepts two parameters as given below as follows.

Returns :
It returns the difference between two specified period values.



Example-1 :
Here, the parameters are used in the format of YYMM and the returned month is negative because period1 is less than period2.

SELECT PERIOD_DIFF(2003, 2011);

Output :

-8

Example-2 :
Here, the parameters are used in the format of YYMM and the returned month is positive because period1 is greater than period2.

SELECT PERIOD_DIFF(2020, 2007);

Output :

13

Example-3 :
Here, the parameters are used in the format of YYYYMM and the returned month is negative because period1 is less than period2.

SELECT PERIOD_DIFF(202001, 202010);

Output :

-9

Example-4 :
Here, the parameters are used in the format of YYYYMM and the returned month is positive because period1 is greater than period2.

SELECT PERIOD_DIFF(202012, 202009);

Output :

3

Application :
This function is used to return the difference between two specified periods.

Article Tags :
SQL