Open In App

PERIOD_ADD () and PERIOD_DIFF () in MariaDB

Last Updated : 12 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

1. PERIOD_ADD Function :

In MariaDB, The PERIOD_ADD () takes a period (formatted as YYMM or YYYYMM) and adds a specified number of months to it. In this function, the first parameter will be a period and the second parameter will be a number. This function will return the result formatted as YYYYMM.

Syntax :

PERIOD_ADD( period, number)

Parameter :

Parameter Description
Period A period formatted as either YYMM or YYYYMM.
Number A total number of months to add to the period. It can be negative or positive

Return :

It will return a period (formatted as YYMM or YYYYMM).

Example-1 :

SELECT PERIOD_ADD(202006, 6);

Output :

202012

Example-2 :

SELECT PERIOD_ADD(201902, -10);

Output :

201804

Example-3 :

SELECT PERIOD_ADD(1806, 5);

Output :

201811

2. PERIOD_DIFF Function :

In MariaDB, The PERIOD_DIFF () is used to return the difference in months between two periods (formatted as YYMM or YYYYMM). In this function, the first parameter will be a period1 and the second parameter will be the period2. The arguments  period1 and period2 must be formatted as either YYYYMM or YYMM, but to be the same format as each other. So period1 could be formatted as YYYYMM then period2 must be formatted as YYYYMM or vice versa.

Syntax :

PERIOD_DIFF( period1, period2 )

Parameters :

Parameters Description
Period1 The first period in which the month of period2 will be subtracted.
Period2 The second period will be subtracted from period 1.

Return :

Returns the difference in months between two periods 

Example-1 :

SELECT PERIOD_DIFF(202005, 202002);

Output :

3

Example-2 :

SELECT PERIOD_DIFF(1908, 1901);

Output :

7

Example-3 :

SELECT PERIOD_DIFF(201903, 201909);

Output :

6

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads