MySQL PERIOD_ADD() Function
MySQL PERIOD_ADD() Function
The MySQL PERIOD_ADD() function adds a specified number of months to an integer period formatted as YYMM or YYYYMM. It always returns the resulting period in YYYYMM integer format.
Financial systems and legacy reporting tables frequently store fiscal months as YYYYMM integers like 202605 instead of DATE columns. PERIOD_ADD() lets you step forward or backward across month and year boundaries without manually converting periods to dates first.
Passing a negative number of months subtracts months cleanly, wrapping across December and January automatically.
Syntax
PERIOD_ADD(P, N)
P: Integer period inYYMMorYYYYMMformat (not aDATEvalue).N: Integer number of months to add (positive or negative).
Practical Example
Suppose demo_orders tracks subscription billing cycles using a billing_period column formatted as YYYYMM. You can compute the next renewal period and the previous period in one query.
SELECT
order_id,
billing_period,
PERIOD_ADD(billing_period, 1) AS next_period,
PERIOD_ADD(billing_period, -3) AS quarter_ago
FROM demo_orders
WHERE billing_period = 202601;
| order_id | billing_period | next_period | quarter_ago |
|---|---|---|---|
| 310 | 202601 | 202602 | 202510 |
| 314 | 202601 | 202602 | 202510 |
Subtracting 3 months from 202601 rolls back to 202510 (October 2025) accurately.
Common Mistakes & NULL Behavior
The biggest mistake with PERIOD_ADD() is passing a DATE column or an 8-digit YYYYMMDD value. PERIOD_ADD('2026-05-10', 1) does not parse as a standard date; MySQL either warns or misreads the digits, producing wrong numbers or NULL. Use DATE_ADD() for DATE columns and PERIOD_ADD() strictly for YYMM or YYYYMM periods.
Two-digit years in YYMM format follow MySQL's century rule: 00 to 69 map to 2000–2069, while 70 to 99 map to 1970–1999. Always store four-digit years (YYYYMM) to avoid ambiguity.
PERIOD_ADD() returns NULL if either P or N is NULL. Passing non-numeric garbage strings can return 0 or NULL with warnings depending on SQL mode.
Frequently Asked Questions
Can PERIOD_ADD() subtract months?
Yes. Pass a negative integer for N, such as PERIOD_ADD(202602, -2), which returns 202512.
Does PERIOD_ADD() work on DATE columns like '2026-05-01'?
No. PERIOD_ADD() expects YYMM or YYYYMM integers. Use DATE_FORMAT(date_col, '%Y%m') first, or use DATE_ADD() on real DATE columns.
Why did PERIOD_ADD(9805, 2) return 199807?
MySQL maps two-digit years 70 through 99 to the 1900s (1998), and always outputs the result in six-digit YYYYMM format.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.