×

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 Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL DATE_FORMAT() function

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

3 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL Character Length Function

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

2 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 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.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL DATEDIFF() function

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

3 minutes read.