如何在 DBSQL 中将时间戳舍入到日期?

How do I round a timestamp to date in DBSQL?

我想将我的时间戳四舍五入到一天。继此 answer。但是我收到一个错误。

Select date_format(timestamp, '%Y-%m-%d') as day

错误:

org.apache.hive.service.cli.HiveSQLException: Error running query: org.apache.spark.SparkException: Job aborted due to stage failure: Task 82 in stage 9847.0 failed 4 times, most recent failure: Lost task 82.3 in stage 9847.0 (TID 225269) (10.139.66.255 executor 2): org.apache.spark.SparkUpgradeException: You may get a different result due to the upgrading of Spark 3.0: Fail to recognize '%Y-%m-%d' pattern in the DateTimeFormatter. 1) You can set legacy_time_parser_policy to LEGACY to restore the behavior before Spark 3.0. 2) You can form a valid datetime pattern with the guide from https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html
    at 0x44ede16 <photon>.ColExprFromProtoImpl(external/workspace_spark_3_2/photon/exprs/date-format-expr.h:92)

使用date_trunc函数。

Select date_trunc('day' ,timestamp) as day, 
Select date_trunc('week' ,timestamp) as week,

for PostgreSQL 适用于 Databricks SQL.

其他功能也仅供参考

> SELECT date_trunc('YEAR', '2015-03-05T09:32:05.359');
 2015-01-01 00:00:00
> SELECT date_trunc('MM', '2015-03-05T09:32:05.359');
 2015-03-01 00:00:00
> SELECT date_trunc('DD', '2015-03-05T09:32:05.359');
 2015-03-05 00:00:00
> SELECT date_trunc('HOUR', '2015-03-05T09:32:05.359');
 2015-03-05 09:00:00
> SELECT date_trunc('MILLISECOND', '2015-03-05T09:32:05.123456');
 2015-03-05 09:32:05.123