// dates & times reference

SQL TO_YMINTERVAL function

Converts a years-months string into an INTERVAL YEAR TO MONTH value.

類別
Dates & times
Returns
interval year to month
In this category
118 of 121

語法

TO_YMINTERVAL('1-6')

引擎支援

引擎Support備註
SQL Server Not built in No built-in equivalent.
PostgreSQL Different name or syntax INTERVAL '1 year 6 months' literal
MySQL Different name or syntax INTERVAL '1-6' YEAR_MONTH compound unit; clamps at month ends
SQLite Different name or syntax no months type; '+18 months' modifiers
Oracle 是 this, or INTERVAL '1-6' YEAR TO MONTH literal

範例

Oracle

SELECT DATE '2026-08-01' + TO_YMINTERVAL('1-6') FROM dual;  -- 2028-02-01

Oracle

SELECT DATE '2026-01-31' + TO_YMINTERVAL('0-1') FROM dual;  -- ORA-01839

MySQL

SELECT DATE_ADD('2026-08-01', INTERVAL '1-6' YEAR_MONTH);  -- 2028-02-01

小知識

  • The format is years-months: TO_YMINTERVAL('1-6') is one year six months, and ISO 'P1Y6M' also works. A plain '18' raises ORA-01867.
  • Oracle does not clamp interval month arithmetic: DATE '2026-01-31' + TO_YMINTERVAL('0-1') raises ORA-01839 (date not valid for month specified). Use ADD_MONTHS when the start date can be a 29th, 30th or 31st.
  • Day-second strings error here; TO_DSINTERVAL is the mirror. A DATE plus either interval type works directly and returns a DATE.

Questions about TO_YMINTERVAL

Why does '2026-08-01' + '1-6' give 2028-02-01?

18 calendar months: August 2026 + 18 months is February 2028, same day. Calendar months, not 30-day blocks; that is the YEAR TO MONTH contract.

What is the PostgreSQL spelling?

PostgreSQL accepts the SQL-standard '1-6' text too: INTERVAL '1-6' is 1 year 6 mons, the same as INTERVAL '18 months'. PostgreSQL clamps at month ends rather than erroring.

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, SQLite, 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.