×

MySQL PERIOD_DIFF() function

In this context, we will learn how we can use the MySQL PERIOD_DIFF() function with proper syntax and good examples.

Introduction of 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 the two period parameters used in this function should be in the same format.

Syntax of the MySQL PERIOD_DIFF() function

The syntax of the MySQL PERIOD_DIFF() function is given as follows:

PERIOD_DIFF(period1, period2)

Parameters or arguments used in MySQL PERIOD_DIFF() function:

There are two parameters accepted by the PERIOD_DIFF() function in MySQL, which are given as follows:

Period1: This is the specified first period in the format of YYMM or YYYYMM.

Period2: The specified second period in the format of YYMM or YYYYMM.

Returns:

It will return the difference between two specified period values.

Application used for PERIOD_DIFF() function:

The PERIOD_DIFF() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL PERIOD_DIFF() function:

Now we will look into some MySQL PERIOD_DIFF() function examples and will explore how we can use the PERIOD_DIFF function in MySQL.

Example-1 :

In this example, the parameters are used in the format of YYMM, and the returned month is negative because the period1 is less than period2.

SELECT PERIOD_DIFF(2002, 2011);

Output:

-9
1 row in set (0.00 sec)

Example-2 :

In this example, the parameters are used in the format of YYMM, and the returned month is positive because the period1 is greater than period2.

SELECT PERIOD_DIFF(2020, 2006);

Output:

14
1 row in set (0.00 sec)

Example-3 :

In this example, the parameters are used in the format of YYYYMM, and the returned month is negative because period 1 is less than period 2.

SELECT PERIOD_DIFF(202005, 202010);

Output:

-5
1 row in set (0.00 sec)

Example-4 :

In this example, the parameters are used in the format of YYYYMM in MySQL, and the returned month is positive because period 1 is greater than period 2.

SELECT PERIOD_DIFF(202014, 202009);

Output:

5
1 row in set (0.00 sec)

Application of MySQL PERIOD_DIFF() function:

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

Summary:

In the above context, we have learned how to use the PERIOD_DIFF() function in MySQL to return the difference between two specified periods.


Related Topics

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL NOW() function

In this context, we will learn how we can use the MySQL NOW() function with proper syntax and good examples. Introduction of MySQL NOW() function Basically, in MySQL, this function is used...

2 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL RADIANS() function

In this context, we will learn how we can use the MySQL RADIANS() function with proper syntax and good examples. Introduction of MySQL RADIANS() function RADIANS() function in MySQL is used to...

2 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.