// dates & times reference

SQL TO_DSINTERVAL function

Returns an INTERVAL value parsed from text: TO_DSINTERVAL for day-hour-minute-second spans, TO_YMINTERVAL for year-month spans.

Kategorie
Dates & times
Returns
INTERVAL DAY TO SECOND / YEAR TO MONTH
In this category
60 of 121

Syntax

TO_DSINTERVAL('0 02:30:00')
TO_YMINTERVAL('01-06')
NUMTODSINTERVAL(150, 'minute')

Each block is a separate form. The support table and the examples show which engine takes which.

Engine-Unterstützung

EngineSupportNotizen
SQL Server Not built in no interval type; DATEADD applies units directly
PostgreSQL Different name or syntax '02:30:00'::interval or INTERVAL '02:30:00' literals
MySQL Different name or syntax INTERVAL 150 MINUTE literals
SQLite Not built in no interval type; date modifiers
Oracle Ja Supported under this name.

Beispiele

Oracle

SELECT d + TO_DSINTERVAL('0 02:30:00') AS later FROM events;

Oracle

SELECT d + TO_YMINTERVAL('01-06') AS in_18_months FROM events;

Oracle

SELECT d + NUMTODSINTERVAL(mins, 'minute') AS later FROM jobs;

Gut zu wissen

  • The SQL format needs the days field: '1 02:30:00' is one day plus 2h30m, and TO_DSINTERVAL('02:30:00') raises ORA-01867. The ISO form 'PT2H30M' is also accepted.
  • Adding a year-month interval does not clamp the way ADD_MONTHS does: DATE '2024-02-29' + TO_YMINTERVAL('01-00') raises ORA-01839 instead of returning February 28.
  • NUMTODSINTERVAL(n, unit) builds the same span from a number, which is the right form when the amount arrives from a column.

Questions about TO_DSINTERVAL

TO_DSINTERVAL or NUMTODSINTERVAL?

TO_DSINTERVAL parses a text literal; NUMTODSINTERVAL builds from a numeric amount and unit. Use the numeric form when the value comes from a column.

Why does TO_DSINTERVAL raise ORA-01867?

The string is missing the days field: write '0 02:30:00', not '02:30:00', or use the ISO form 'PT2H30M'.

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.