// dates & times reference

SQL NUMTOYMINTERVAL function

Converts a numeric value in years or months into an INTERVAL YEAR TO MONTH.

Kategorie
Dates & times
Returns
interval year to month
In this category
120 of 121

Syntax

NUMTOYMINTERVAL(n, 'YEAR' | 'MONTH')

Engine-Unterstützung

EngineSupportNotizen
SQL Server Not built in No built-in equivalent.
PostgreSQL Different name or syntax make_interval(months => n)
MySQL Different name or syntax INTERVAL n MONTH keyword form
SQLite Not built in '+' || n || ' months' modifier
Oracle Ja this, but unlike ADD_MONTHS it errors at month ends

Beispiele

Oracle

SELECT ADD_MONTHS(SYSDATE, -6) FROM dual;  -- matches SYSDATE + NUMTOYMINTERVAL(-6, 'MONTH') except near month ends

Oracle

SELECT SYSDATE + NUMTOYMINTERVAL(18, 'MONTH') FROM dual;

PostgreSQL

SELECT now() + make_interval(months => 18);

Gut zu wissen

  • Two units only: YEAR and MONTH. Fractions round to whole months: NUMTOYMINTERVAL(1.5, 'MONTH') is 2 months and NUMTOYMINTERVAL(1.4, 'MONTH') is 1, while NUMTOYMINTERVAL(1.5, 'YEAR') is 1 year 6 months.
  • Adding to a month end errors: DATE '2026-01-31' + NUMTOYMINTERVAL(1, 'MONTH') raises ORA-01839, where ADD_MONTHS(DATE '2026-01-31', 1) returns 2026-02-28.
  • ADD_MONTHS also promotes month ends (2026-02-28 + 1 month gives 2026-03-31); the interval add gives 2026-03-28. Pick the spelling by the month-end behaviour you need.

Questions about NUMTOYMINTERVAL

Why pick NUMTOYMINTERVAL over ADD_MONTHS?

It produces an interval value you can store, add, average and compare; ADD_MONTHS only ever returns a shifted date. For contract lengths in columns, the interval is the data model.

What about half months?

Not representable: the type is whole months. Express 15 days as a DAY TO SECOND interval instead, and accept that the two types cannot be added without casting to a finer granularity.

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: PostgreSQL, MySQL, Oracle.

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