// dates & times reference

SQL TIMEDIFF function

Returns dt1 minus dt2 as a TIME value, capped at the range TIME allows.

Catégorie
Dates & times
Returns
time
In this category
72 of 121

Syntaxe

TIMEDIFF(dt1, dt2)

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Different name or syntax no TIMEDIFF; DATEDIFF(SECOND, ...) or cast arithmetic
PostgreSQL Different name or syntax dt1 - dt2 returns an interval
MySQL Oui Supported under this name.
SQLite Not built in No built-in equivalent.
Oracle Different name or syntax dt1 - dt2 returns INTERVAL DAY TO SECOND

Exemples

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

Bon à savoir

  • 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.

Cheat sheets for the engines that have it: SQL Server, 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.