PERIOD_ADD() function in MySQL
PERIOD_ADD() function in MySQL helps to add a specific number of months to a given period. The PERIOD_ADD() function will return the resultant value in ‘YYYYMM‘ format.
Syntax :
PERIOD_ADD(period, number)
Parameters :
-
period –
A period which should be in YYMM OR YYYYMM format.
-
number –
The number of months which will be added to a given period, the value can be negative or positive.
Result :
The function will return the resultant value after adding a specific number of months to the given period.
Example-1 :
Adding months to a given period using PERIOD_ADD() function.
SELECT PERIOD_ADD(202011, 9) As New_period;
Output :
Example-2 :
Subtracting months from a given period using PERIOD_ADD() function.
SELECT PERIOD_ADD(202102, -5) As New_period;
Output :
Example-3 :
Adding and Subtracting months from a two-digit year period.
SELECT
PERIOD_ADD(2109, -5) As New_period1,
PERIOD_ADD(2109, +5) As New_period2;
Output :
New_period1 |
New_period2 |
202104 |
202202 |
Example-4 :
Using the Current Date and Extract functions.
SELECT
CURDATE( ) AS 'Curr_date',
EXTRACT(YEAR_MONTH FROM CURDATE( )) AS 'Curr_period',
PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE( )), 11) AS 'New_period';
Output :
Curr_date |
Curr_period |
New_period |
2020-11-30 |
202011 |
202110 |
Last Updated :
02 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...