Skip to main content

TIMEDIFF

Returns the difference between two time values as a string of the form (+|-)YYYY-MM-DD HH:MM:SS.SSS, the amount of time that must be added to the second value to reach the first.

See also:
[SELECT] [JULIANDAY] [UNIXEPOCH]


Syntax​

SELECT TIMEDIFF(time_value1, time_value2) FROM <multipartIdentifier>;

Arguments​

time_value1
The later time value, in any format the date and time functions accept.

time_value2
The earlier time value.

Return Value(s)​

Returns a string, negative when time_value1 is earlier than time_value2. The result is NULL for an invalid time value.


Examples​

Differences between time values​

The month part depends on the calendar, so the reversed difference is not the same magnitude:

SELECT TIMEDIFF('2024-03-01', '2024-01-15') AS a,
TIMEDIFF('2024-01-15', '2024-03-01') AS b,
TIMEDIFF('2024-01-01 12:00:00', '2024-01-01 09:30:15') AS c;
|--------------------------|--------------------------|--------------------------|
| a | b | c |
|--------------------------|--------------------------|--------------------------|
| +0000-01-15 00:00:00.000 | -0000-01-17 00:00:00.000 | +0000-00-00 02:29:45.000 |
|--------------------------|--------------------------|--------------------------|

Age of a resource​

The following query shows how long after creation each repository in a GitHub organization last received a push:

SELECT name, TIMEDIFF(pushed_at, created_at) AS active_for
FROM github.repos.repos
WHERE org = 'stackql-labs';

For more information, see https://www.sqlite.org/lang_datefunc.html