1

我想将我的时间戳四舍五入到当天。按照这个答案。但我收到一个错误。

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)
4

2 回答 2

1

使用date_trunc功能。

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

PostgreSQL 的这个答案适用于 Databricks SQL。

于 2021-12-09T16:11:40.350 回答
1

其他功能也仅供参考。

> 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
于 2021-12-09T17:46:24.277 回答