// dates & times reference

SQL NUMTODSINTERVAL function

Converts a numeric value in a named unit into an INTERVAL DAY TO SECOND, built for column-driven durations.

Категория
Dates & times
Returns
interval day to second
In this category
119 of 121

Синтаксис

NUMTODSINTERVAL(n, 'unit')

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

ДвижокSupportЗаметки
SQL Server Not built in No built-in equivalent.
PostgreSQL Different name or syntax make_interval(hours => n) for the multi-part form
MySQL Different name or syntax INTERVAL n unit keyword
SQLite Not built in modifier strings built with ||
Oracle Да this: accepts DAY, HOUR, MINUTE, SECOND

Примеры

Oracle

SELECT SYSDATE + NUMTODSINTERVAL(90, 'MINUTE') FROM dual;

Oracle

SELECT NUMTODSINTERVAL(1.5, 'DAY') FROM dual;  -- +01 12:00:00

PostgreSQL

SELECT make_interval(days => 1, hours => 12);  -- same span

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

  • Only four units: DAY, HOUR, MINUTE, SECOND. Weeks and months are year-to-month territory: NUMTOYMINTERVAL's job.
  • The number can be fractional or a column: NUMTODSINTERVAL(elapsed_seconds / 3600, 'HOUR'). The reason it exists over string-built literals.
  • Fractional results carry down: NUMTODSINTERVAL(1.5, 'DAY') is 1 day 12:00:00; NUMTODSINTERVAL(90, 'MINUTE') is 00 01:30:00.

Questions about NUMTODSINTERVAL

NUMTODSINTERVAL vs adding days numerically?

On a DATE, d + 90/1440 works in fractions of a day. On a TIMESTAMP, + 90/1440 silently converts the result to DATE and drops fractional seconds, while + NUMTODSINTERVAL(90, 'MINUTE') keeps it a TIMESTAMP.

What is the PostgreSQL equivalent?

make_interval with named parts, or INTERVAL '90 minutes' literals when the value is static.

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.