Back to Materialize

date_trunc function

doc/user/content/sql/functions/date-trunc.md

1231.3 KB
Original Source

date_trunc computes ts_val's "floor value" of the specified time component, i.e. the largest time component less than or equal to the provided value.

To align values along arbitrary values, see date_bin.

Signatures

{{% include-syntax file="examples/sql_functions/date_trunc" example="syntax" %}}

ParameterTypeDescription
valtimestamp, timestamp with time zone, intervalThe value you want to truncate.

Return value

date_trunc returns the same type as val.

Examples

mzsql
SELECT date_trunc('hour', TIMESTAMP '2019-11-26 15:56:46.241150') AS hour_trunc;
nofmt
          hour_trunc
-------------------------------
 2019-11-26 15:00:00.000000000
mzsql
SELECT date_trunc('year', TIMESTAMP '2019-11-26 15:56:46.241150') AS year_trunc;
nofmt
          year_trunc
-------------------------------
 2019-01-01 00:00:00.000000000
mzsql
SELECT date_trunc('millennium', INTERVAL '1234 years 11 months 23 days 23:59:12.123456789') AS millennium_trunc;
nofmt
          millennium_trunc
-------------------------------
 1000 years