MySQL: PERIOD_DIFF Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL PERIOD_DIFF Function

In this guide, we will explain how to use the MySQL PERIOD_DIFF function with syntax and examples.

Description

The MySQL PERIOD_DIFF function returns the difference in months between two periods (formatted as YYMM or YYYYMM).

Syntax

The syntax for the PERIOD_DIFF function in MySQL is:

PERIOD_DIFF( period1, period2 )

Parameters or Arguments

period1

A period formatted as either YYMM or YYYYMM.period2A period formatted as either YYMM or YYYYMM.

Note

  • The parameters period1 and period2 must be formatted as either YYMM or YYYYMM, but do not have to be the same format as each other. So period1 could be formatted as YYMM and period2 could be formatted as YYYYMM, or vice versa.

Applies To

The PERIOD_DIFF function can be used in the following 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

Example

Let’s look at some PERIOD_DIFF function examples and explore how to use the PERIOD_DIFF function.

For example:

mysql> SELECT PERIOD_DIFF(201405,201402);
Result: 3

mysql> SELECT PERIOD_DIFF(201508,201408);
Result: 12

mysql> SELECT PERIOD_DIFF(201408,201508);
Result: -12

mysql> SELECT PERIOD_DIFF(1405,1402);
Result: 3

mysql> SELECT PERIOD_DIFF(201411,1409);
Result: 2

mysql> SELECT PERIOD_DIFF(1412,201405);
Result: 7

Next Topic : Click Here

This Post Has One Comment

Leave a Reply