Open In App

PERIOD_DIFF() Function in MySQL

Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • period1 – Specified first period in the format of YYMM or YYYYMM.
  • period2 – Specified second period in the format of YYMM or YYYYMM.

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.


Last Updated : 02 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads