SQL justify_interval function
Normalizes an interval by carrying 24-hour blocks into days and 30-day blocks into months.
- カテゴリ
- Dates & times
- Returns
interval- In this category
- 117 of 121
構文
justify_interval(iv)
justify_hours(iv)
justify_days(iv)
Each block is a separate form. The support table and the examples show which engine takes which.
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | はい | Supported under this name. |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Different name or syntax | DAY TO SECOND intervals auto-normalize on output |
例
PostgreSQL
SELECT justify_hours(INTERVAL '27 hours'); -- 1 day 03:00:00
PostgreSQL
SELECT justify_interval(INTERVAL '45 days'); -- 1 mon 15 days
PostgreSQL
SELECT justify_interval(avg(ended - started)) FROM jobs;
知っておくと便利なこと
- justify_hours carries 24-hour blocks into days: INTERVAL '27 hours' becomes 1 day 03:00:00. justify_days carries 30-day blocks into months. justify_interval does both and also fixes mixed signs.
- The 30-day month is an assumption, not a calendar rule: justify_days(INTERVAL '45 days') gives 1 mon 15 days. Useful for display, wrong for billing periods.
- AVG on intervals is where these shine: raw averages leave giant hours fields; justify_interval(avg(duration)) reads like a human wrote it.
Questions about justify_interval
Why does my interval show 9000 hours?
SUM, AVG and multiplication keep hours in the time field: summing 400 shifts of 22:30:00 gives 9000:00:00. justify_interval carries that into days and months for display.
Does Oracle need this?
No. INTERVAL DAY TO SECOND values normalize on creation and display; there is no raw uncarried state to fix.
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, 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.