×

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

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 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 Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL MONTHNAME() function

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

3 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL QUARTER() function

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

3 minutes read.

MySQL LENGTH() Function

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

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 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 Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 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 String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

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