time_diff function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 18.3 and above

Returns the difference between two time values measured in units.

Syntax

time_diff(unit, start, end)

unit
 { MICROSECOND |
   MILLISECOND |
   SECOND |
   MINUTE |
   HOUR }

Arguments

  • unit: A unit of measure.
  • start: A starting TIME expression.
  • end: An ending TIME expression.

Returns

A BIGINT.

If start is greater than end the result is negative.

Examples

> SELECT time_diff(HOUR, TIME'08:00:00', TIME'21:30:00');
  13

> SELECT time_diff(MINUTE, TIME'08:00:00', TIME'08:45:30');
  45

> SELECT time_diff(SECOND, TIME'10:00:00', TIME'10:00:30');
  30

-- Start is greater than end
> SELECT time_diff(HOUR, TIME'21:00:00', TIME'08:00:00');
  -13