SQL ADD_MONTHS function
Adds whole months to a date. Its signature behaviour: a month-end date in yields a month-end date out.
- Категория
- Dates & times
- Returns
date- In this category
- 67 of 121
Синтаксис
ADD_MONTHS(date, n)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Different name or syntax | DATEADD(MONTH, n, d); no month-end preservation |
| PostgreSQL | Different name or syntax | d + n * INTERVAL '1 month'; clamps, does not preserve month ends |
| MySQL | Different name or syntax | DATE_ADD(d, INTERVAL n MONTH); clamps |
| SQLite | Different name or syntax | date(d, '+' || n || ' months') |
| Oracle | Да | Supported under this name. |
Примеры
Oracle
SELECT ADD_MONTHS(DATE '2026-01-31', 1) FROM dual; -- 2026-02-28
Oracle
SELECT ADD_MONTHS(DATE '2026-02-28', 1) FROM dual; -- 2026-03-31
PostgreSQL
SELECT DATE '2026-02-28' + INTERVAL '1 month'; -- 2026-03-28
Полезно знать
- ADD_MONTHS(DATE '2026-02-28', 1) returns 2026-03-31, because 2026-02-28 is the last day of February. PostgreSQL and MySQL would return 2026-03-28, the biggest practical difference between them.
- ADD_MONTHS(DATE '2026-01-31', 1) returns 2026-02-28, same as the others; the rule only bites when the input is itself a month end.
- The second argument is a whole number; fractional months are truncated before the add.
Questions about ADD_MONTHS
What is the equivalent of ADD_MONTHS in PostgreSQL?
d + INTERVAL '1 month'. Watch the month-end rule though: PostgreSQL never promotes 2026-02-28 to 2026-03-31.
How do I match Oracle's month-end behaviour elsewhere?
Add one month, then check whether the input day exceeded the result month's length; or compute last_day(input) first and add from there.
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: Oracle, SQL Server, PostgreSQL, MySQL, SQLite.
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.