SQL MAKE_INTERVAL function
Returns an interval assembled from up to seven optional integer or double arguments, instead of writing INTERVAL '1 day' text literals.
- Categoría
- Dates & times
- Returns
interval- In this category
- 33 of 121
Sintaxis
MAKE_INTERVAL(years => 1, months => 2)
MAKE_INTERVAL(days => 3, hours => 4)
MAKE_INTERVAL(1, 2, 0, 3, 4, 5, 0)
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | no interval type; DATEADD applies parts directly |
| PostgreSQL | Sí | Supported under this name. |
| MySQL | Different name or syntax | INTERVAL expr unit builds spans from expressions |
| SQLite | Not built in | no interval type; date modifiers |
| Oracle | Different name or syntax | NUMTODSINTERVAL and NUMTOYMINTERVAL from numbers |
Ejemplos
PostgreSQL
SELECT now() + make_interval(days => 3, hours => 4) AS deadline;
PostgreSQL
SELECT make_interval(years => 1, months => 2) AS tenure;
Oracle
SELECT d + NUMTODSINTERVAL(4, 'hour') FROM events;
Conviene saberlo
- All arguments are optional and positional order is years, months, weeks, days, hours, mins, secs; named arguments avoid the classic skipped-field bug.
- Months and days do not convert: make_interval(months => 1, days => 30) stays mixed, and adding it to a date applies the months first, then the days.
- Oracle's NUMTODSINTERVAL is the closest shape: it also builds from numbers, but only days-to-seconds or years-to-months spans.
Questions about MAKE_INTERVAL
Why use make_interval over INTERVAL '1 day'?
When the amounts come from columns or parameters, composing a text literal needs format gymnastics; make_interval takes integers directly.
Does make_interval normalize units?
No. 1 month plus 30 days stays as written; only when it is added to a date do the calendar rules decide the result.
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: PostgreSQL, MySQL, Oracle.
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.