SQL PERIOD_ADD function
Arithmetic on month-period integers in the YYYYMM form, with no day or time component at all.
- Catégorie
- Dates & times
- Returns
integer (YYYYMM / months)- In this category
- 80 of 121
Syntaxe
PERIOD_ADD(period, n)
PERIOD_DIFF(period1, period2)
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Oui | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
Exemples
MySQL
SELECT PERIOD_ADD(202608, 3); -- 202611
MySQL
SELECT PERIOD_DIFF(202608, 202601); -- 7
Bon à savoir
- Arguments are integers like 202608, not dates: PERIOD_ADD(202608, 3) gives 202611. A full YYYYMMDD value is not rejected: PERIOD_ADD(20260801, 1) returns 20260802, which looks plausible and is wrong.
- Two-digit years are accepted with MySQL's 70-99 / 00-69 pivot (1970-1999, 2000-2069); four-digit years are safer.
- For real dates use DATE_ADD(d, INTERVAL n MONTH) and TIMESTAMPDIFF(MONTH, a, b); PERIOD functions exist for accounting-style period columns.
Questions about PERIOD_ADD / PERIOD_DIFF
How do I convert a date to a period?
EXTRACT(YEAR_MONTH FROM d) returns 202608 for any August 2026 date. DATE_FORMAT(d, '%Y%m') + 0 does the same through a string.
Does PERIOD_DIFF count month boundaries like DATEDIFF?
No. It is a true month difference of period numbers: PERIOD_DIFF(202601, 202512) is 1 regardless of days.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: MySQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.