MySQL PERIOD_ADD() Function
The PERIOD_ADD()
function in MySQL adds a specified number of months to a given period. A period is represented as YYYYMM or YYMM, where YYYY is the year and MM is the month.
PERIOD_ADD(): Definition and Usage
This function is helpful for calculations involving periods, such as adding months to a specific date to find a future date. The result is always formatted as YYYYMM.
Syntax
Syntax
PERIOD_ADD(period, number)
Parameter Values
Parameter | Description |
---|---|
period |
The starting period (YYYYMM or YYMM). This is required. |
number |
The number of months to add (positive) or subtract (negative). This is required. |
Examples
Adding Months to a Period
This example adds 5 months to March 2017.
Syntax
SELECT PERIOD_ADD(201703, 5);
Output
201708
Adding More Months
Adding 15 months to March 2017.
Syntax
SELECT PERIOD_ADD(201703, 15);
Output
201806
Subtracting Months
Subtracting 2 months from March 2017 (using a negative number).
Syntax
SELECT PERIOD_ADD(201703, -2);
Output
201701