【问题标题】:How do I round a timestamp to date in DBSQL?如何在 DBSQL 中将时间戳舍入到日期?
【发布时间】:2021-12-09 16:11:40
【问题描述】:

我想将我的时间戳四舍五入到当天。关注此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)

【问题讨论】:

    标签: databricks databricks-sql dbsql


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      使用date_trunc 函数。

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

      This answer 适用于 PostgreSQL,适用于 Databricks SQL。

      【讨论】:

        猜你喜欢
        • 2015-02-04
        • 2021-09-10
        • 2015-02-27
        • 2016-03-07
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        相关资源
        最近更新 更多