MySQL Date/Time period_diff() Function

Last Updated : 26 Feb 2026

The period_diff() is a MySQL date/time function. It is used to get the difference between two given periods.

In this article, we will understand the concept of the MySQL period_diff() function in detail with its various working examples.

Syntax

Parameter:

period1 : period value in format YYMM or YYYYMM.

period2 : period value in format YYMM or YYYYMM.

Returns:

This function returns the difference in months between two periods.

Example 1

Explanation: On executing the above SELECT statement, the period_diff() function is used to return a difference in number of months.

Output: The output of this example is given below.

MySQL Datetime period_diff() Function

Example 2

Explanation: On executing the above SELECT statement, the period_diff() function is used to return a difference in number of months.

Output: The output of this example is given below.

MySQL Datetime period_diff() Function

Example 3: PERIOD_ADD function with NULL Value as a period values.

Explanation: On executing the above SELECT statement, the period_diff() function returns NULL, if either the value of period or the number is NULL.

Output: The output of this example is given below.

RESULT
NULL

Example 4: PERIOD_DIFF function with Negative Value.

Explanation: On executing the above SELECT statement, the period_diff() function will subtract negative values and return the resulting period.

Output: The output of this example is given below.

RESULT
-2

Using the MySQL DateTime period_diff() function in a Table column

We can use the period_diff() function in a table to find the difference between the given dates in the specified format YYYYMM or YYMM.

Following the steps given below to implement the MySQL DateTime period_diff() function in a column of the table.

Step 1: First, create a table named Emp_Details using CREATE TABLE statement.

Step 2: After that INSERT the data into a table using the INSERT TABLE statement.

Step 3: To check the information with the use of SELECT statement, whether the data is inserted or not.

EIDNameDepartmentJoining_DateReleiving_DateSalary
1Anjana GuptaClothes20230520251015000
2Priyanka SharmaAccessories20230820251015000
3Bobby KapoorFood20231020251025000
4Alisha SharmaFinance20231220251060000
5Eshant SharmaClothes20240220251010000
6Ravi RathoreFood20240620251014500
7Ravi SharmaSanitary20240820251025000
8Sanjay SharmaAccessories20241020251010000

Example:

Explanation: On execution of the above SELECT query, the MYSQL period_diff() function in a Emp_Details to find the difference between the Joining_Date and Releiving_Date in the format YYYYMM or YYMM.

Output: The output of this example is given below.

EIDNameDepartmentJoining_DateReleiving_DateSalaryResult
1Anjana GuptaClothes2023052025101500029
2Priyanka SharmaAccessories2023082025101500026
3Bobby KapoorFood2023102025102500024
4Alisha SharmaFinance2023122025106000022
5Eshant SharmaClothes2024022025101000020
6Ravi RathoreFood2024062025101450016
7Ravi SharmaSanitary2024082025102500014
8Sanjay SharmaAccessories2024102025101000012

Using the MySQL DateTime period_diff() function with WHERE clause

We can use the period_diff() function in a table to find the difference between the given dates in the specified format YYYYMM or YYMM. By using WHERE clause we can get restricted data from the given column values.

Sample Table: Order_Details

OIDNameDepartmentJoining_DateReleiving_DateSalary
1Anjana GuptaClothes20230520251015000
2Priyanka SharmaAccessories20230820251015000
3Bobby KapoorFood20231020251025000
4Alisha SharmaFinance20231220251060000
5Eshant SharmaClothes20240220251010000
6Ravi RathoreFood20240620251014500
7Ravi SharmaSanitary20240820251025000
8Sanjay SharmaAccessories20241020251010000

Example:

Explanation: On execution of the above SELECT query, the MYSQL period_diff() function in a Emp_Details to find the difference between the Releiving_Date and the Joining_Date in the format YYYYMM or YYMM where the value of the Salary column is greater than 10000.

Output: The output of this example is given below.

EIDNameDepartmentJoining_DateReleiving_DateSalaryResult
1Anjana GuptaClothes2023052025101500029
2Priyanka SharmaAccessories2023082025101500026
3Bobby KapoorFood2023102025102500024
4Alisha SharmaFinance2023122025106000022
6Ravi RathoreFood2024062025101450016
7Ravi SharmaSanitary2024082025102500014

MySQL DateTime period_diff() function with UPDATE Statement

In MySQL, the period_diff() function does not change the data in the table. With the use UPDATE statement we can update the data.

Take an example of the Emp_Details table, which contains the following information.

EIDNameDepartmentJoining_DateReleiving_DateSalary
1Anjana GuptaClothes20230520251015000
2Priyanka SharmaAccessories20230820251015000
3Bobby KapoorFood20231020251025000
4Alisha SharmaFinance20231220251060000
5Eshant SharmaClothes20240220251010000
6Ravi RathoreFood20240620251014500
7Ravi SharmaSanitary20240820251025000
8Sanjay SharmaAccessories20241020251010000

Syntax:

Example:

Step1: First add a new column i.e. Result to the existing table using the ALTER statement.

Step2: After that use a UPDATE statement to set the value of the Result column.

Explanation: On execution of the above UPDATE query, it will update the records in the Emp_Details table. If you want to check that the content is updated in the table or not, then use the following statement.

Output: After running this query, the output of the table is given below.

EIDNameDepartmentJoining_DateReleiving_DateSalaryResult
1Anjana GuptaClothes2023052025101500029
2Priyanka SharmaAccessories2023082025101500026
3Bobby KapoorFood2023102025102500024
4Alisha SharmaFinance2023122025106000022
5Eshant SharmaClothes2024022025101000020
6Ravi RathoreFood2024062025101450016
7Ravi SharmaSanitary2024082025102500014
8Sanjay SharmaAccessories2024102025101000012

Next TopicMySQL datetime