Mysql Period_diff Function

MySQL: PERIOD_DIFF Function

This tutorial explains 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.

period2

A 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 of MySQL:

  • 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 MySQL PERIOD_DIFF function examples and explore how to use the PERIOD_DIFF function in MySQL.

For example:

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

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

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

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

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

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