MySQL PERIOD_ADD() Function
The MySQL PERIOD_ADD() function adds a specified number of months to a period. It takes period formatted as YYMM or YYYYMM and returns the result formatted as YYYYMM.
Syntax
PERIOD_ADD(period, number)
Parameters
period |
Required. Specify the period formatted as either YYMM or YYYYMM. |
number |
Required. Specify the number of months to add to the period. It can be a positive or negative value. |
Return Value
Returns the period after adding specified number of months.
Example 1:
The example below shows the usage of PERIOD_ADD() function.
mysql> SELECT PERIOD_ADD(201808, 4); Result: 201812 mysql> SELECT PERIOD_ADD(201808, -4); Result: 201804 mysql> SELECT PERIOD_ADD(201808, 9); Result: 201905 mysql> SELECT PERIOD_ADD(201808, -9); Result: 201711 mysql> SELECT PERIOD_ADD(1808, 4); Result: 201812 mysql> SELECT PERIOD_ADD(1808, -4); Result: 201804 mysql> SELECT PERIOD_ADD(1808, 9); Result: 201905 mysql> SELECT PERIOD_ADD(1808, -9); Result: 201711
Example 2:
Consider a database table called Sample with the following records:
Data | Period | Month |
---|---|---|
Data 1 | 201404 | 6 |
Data 2 | 201504 | 7 |
Data 3 | 201604 | 8 |
Data 4 | 201704 | 9 |
Data 5 | 201804 | 10 |
To create a period based on adding values of column Period and column Month, the following query can be used:
SELECT *, PERIOD_ADD(Period, Month) AS PERIOD_ADD_Value FROM Sample;
This will produce the result as shown below:
Data | Period | Month | PERIOD_ADD_Value |
---|---|---|---|
Data 1 | 201404 | 6 | 201410 |
Data 2 | 201504 | 7 | 201511 |
Data 3 | 201604 | 8 | 201612 |
Data 4 | 201704 | 9 | 201801 |
Data 5 | 201804 | 10 | 201902 |
❮ MySQL Functions