【问题标题】:How to convert date in YYYYMMDD format (int) to datetime format in hive?如何将 YYYYMMDD 格式(int)的日期转换为配置单元中的日期时间格式?
【发布时间】:2019-12-11 13:10:04
【问题描述】:
 select from_unixtime(unix_timestamp('20190901','yyyymmdd'));
OK
+----------------------+
|         _c0          |
+----------------------+
| 2019-01-01 00:09:00  |
+----------------------+

所需的输出是 2019-09-01 00:00:00 。我做错了什么?

【问题讨论】:

  • 不确定如何格式化显示

标签: sql date datetime hive hiveql


【解决方案1】:

想通了。 mm 是分钟,MM 是月。

应该是 yyyyMMdd 而不是 yyyymmdd

【讨论】:

    【解决方案2】:

    高效的方法是使用substr和concat_ws:

     with your_table as (select 20190901 as initial_date)
       select concat_ws('-',substr(initial_date, 1, 4),  substr(initial_date, 5, 2), substr(initial_date, 7, 2)) as dt from your_table;
    

    结果

    2019-09-01
    

    或者使用 regexp_replace:

       select regexp_replace(initial_date, '^(\\d{4})(\\d{2})(\\d{2})$','$1-$2-$3')
    

    如果您更喜欢使用 unix_timestamp,请使用正确的格式 'yyyyMMdd':

     select from_unixtime(unix_timestamp('20190901','yyyyMMdd'));
    

    在此处查看数据格式:SimpleDateFormat

    【讨论】:

      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2017-12-02
      • 2012-02-18
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多