// dates & times reference

SQL PERIOD_ADD function

Arithmetic on month-period integers in the YYYYMM form, with no day or time component at all.

Категория
Dates & times
Returns
integer (YYYYMM / months)
In this category
80 of 121

Синтаксис

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.

Поддержка движка

ДвижокSupportЗаметки
SQL Server Not built in No built-in equivalent.
PostgreSQL Not built in No built-in equivalent.
MySQL Да Supported under this name.
SQLite Not built in No built-in equivalent.
Oracle Not built in No built-in equivalent.

Примеры

MySQL

SELECT PERIOD_ADD(202608, 3);  -- 202611

MySQL

SELECT PERIOD_DIFF(202608, 202601);  -- 7

Полезно знать

  • 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.

Cheat sheets for the engines that have it: MySQL.

// fewer lookups

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.