SQL TIMEDIFF function
Returns dt1 minus dt2 as a TIME value, capped at the range TIME allows.
- Категория
- Dates & times
- Returns
time- In this category
- 72 of 121
Синтаксис
TIMEDIFF(dt1, dt2)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Different name or syntax | no TIMEDIFF; DATEDIFF(SECOND, ...) or cast arithmetic |
| PostgreSQL | Different name or syntax | dt1 - dt2 returns an interval |
| MySQL | Да | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Different name or syntax | dt1 - dt2 returns INTERVAL DAY TO SECOND |
Примеры
MySQL
SELECT TIMEDIFF('2026-08-01 12:00:00', '2026-08-01 10:30:00'); -- 01:30:00
PostgreSQL
SELECT TIME '12:00:00' - TIME '10:30:00'; -- 01:30:00
Полезно знать
- The result is a TIME, so a span over 838:59:59 (about 35 days) is clipped to that value with only a warning; use TIMESTAMPDIFF(SECOND, ...) for long spans.
- Both arguments must be the same kind: two times, or two datetimes. Mixing time and datetime yields NULL.
- Negative spans are fine: TIMEDIFF('10:00:00', '12:30:00') gives -02:30:00.
Questions about TIMEDIFF
How do I time a long job in MySQL?
TIMESTAMPDIFF(SECOND, started, ended); TIMEDIFF's TIME result maxes out near 35 days and silently clips beyond that.
What does PostgreSQL return for time subtraction?
time - time gives an interval such as 01:30:00; timestamp - timestamp gives an interval that can span days, such as 33 days 03:00:00.
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: SQL Server, 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.