Interval types (spans of time)
'How long' is data: PostgreSQL and Oracle give durations a real type, while SQL Server, MySQL and SQLite store a number and remember which unit it counts.
- Family
- Other types
- Missing in
- SQL Server, MySQL, SQLite
- Gotchas
- 5 listed
Engine by engine
Only PostgreSQL and Oracle have interval column types (with different field structures); elsewhere a duration is an integer of undocumented unit plus DATEADD-style arithmetic at the edges.
| Движок | Тип | Хранилище | Заметки |
|---|---|---|---|
| SQL Server |
none (numeric or datetime math)
Workaround
|
4-8 bytes | No interval type. Durations live as INT/BIGINT seconds or DECIMAL hours, or as the difference of two DATETIME2s via DATEDIFF; DATEADD applies them back. |
| PostgreSQL |
INTERVAL
|
16 bytes | One type covering years to microseconds, internally months/days/seconds fields; '1 month' is stored as a month, not a fixed number of days. |
| MySQL |
none (INTERVAL keyword only)
Workaround
|
н/д | INTERVAL 1 DAY exists only inside expressions (DATE_ADD, DATE_SUB); there is no interval column type. Store numeric seconds or two DATETIME columns. |
| SQLite |
none (numeric)
Workaround
|
TEXT/REAL/INT | No interval type; date math goes through modifiers ('+3 days') and julianday() differences. Store seconds (INT), days (REAL), or ISO 8601 duration text like 'P1DT2H' and interpret it yourself. |
| Oracle |
INTERVAL YEAR TO MONTH / DAY TO SECOND
|
5 / 11 bytes | Two ANSI interval types with separate jobs: calendar spans (years/months) and exact spans (days/seconds). They cannot be added to each other directly. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- PostgreSQL intervals are three fields, not a number of seconds: '1 month' + date arithmetic is calendar-aware (Jan 31 + 1 month lands Feb 28/29), so interval comparisons and sums can surprise ('1 month 30 days' vs '2 months').
- Oracle splits intervals in two on purpose: months are not fixed lengths, so INTERVAL YEAR TO MONTH covers calendar spans and INTERVAL DAY TO SECOND exact ones; casting between them needs a date as the bridge.
- MySQL's INTERVAL keyword only works inside date arithmetic: a column declared INTERVAL is a syntax error. Durations end up as integers, so put the unit in the column name (duration_ms).
- SQL Server DATEDIFF counts boundaries crossed, not elapsed time: DATEDIFF(year, '2025-12-31', '2026-01-01') returns 1 for a one-day span, and DATEDIFF(hour, '10:59', '11:00') returns 1 for one minute.
- SQLite's '+1 month' modifier overflows instead of clamping: date('2026-01-31', '+1 month') returns 2026-03-03. ISO 8601 durations (P1Y2M) stored as text need application-side parsing to compare or sum.
Common questions
What is the difference between PostgreSQL's INTERVAL and Oracle's two types?
PostgreSQL's INTERVAL mixes months, days and seconds in one value (calendar-aware month arithmetic). Oracle forces you to choose INTERVAL YEAR TO MONTH for calendar spans or INTERVAL DAY TO SECOND for exact durations, and the two do not mix directly.
How do I store durations in MySQL or SQL Server?
Pick a unit and document it: INT seconds (or BIGINT milliseconds) is the portable answer. SQL Server can also persist start/end DATETIME2 pairs and compute with DATEDIFF; MySQL uses TIMESTAMPDIFF over two DATETIMEs.
What happens when I add one month to January 31?
It depends on the engine. PostgreSQL, SQL Server DATEADD, MySQL DATE_ADD and Oracle ADD_MONTHS clamp to Feb 28 (or 29). Oracle's DATE + INTERVAL '1' MONTH raises ORA-01839, and SQLite's '+1 month' rolls over to March 3. Test month ends.
Related on SQLly
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.