Skip to main content

time_trunc function

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

Returns time truncated to the unit specified in unit.

Syntax

time_trunc(unit, expr)

Arguments

  • unit: A STRING literal.
  • expr: A TIME expression.

Returns

A TIME.

Valid units for unit are (case-insensitive):

  • 'HOUR': Zero out the minute, second, and fraction parts.
  • 'MINUTE': Zero out the second and fraction parts.
  • 'SECOND': Zero out the fractional second part.
  • 'MILLISECOND': Zero out sub-millisecond fractions.
  • 'MICROSECOND': TIME values are stored at microsecond precision, so no sub-microsecond fractions exist to zero out. The value is unchanged.

Examples

SQL
> SELECT time_trunc('HOUR', TIME'13:08:15.123');
13:00:00

> SELECT time_trunc('MINUTE', TIME'13:08:15.123');
13:08:00

> SELECT time_trunc('SECOND', TIME'13:08:15.123456');
13:08:15

> SELECT time_trunc('MILLISECOND', TIME'13:08:15.123456');
13:08:15.123000