我有一个年月格式的数字字段:
201801
201802
201803
etc
我想将这些转换为日期,只需使用本月的第一天:
20180101
20180201
20180301
etc
进入堆栈溢出兔子洞并尝试了几件事,最终在失败之前走到了这一步:
select to_date(concat(cast(cast(201809 as BIGINT) as STRING), "01")) # returns null
如何将例如 201801 转换为 2018 年 1 月 1 日的日期,格式为 yyyymmdd,即 20180101?
Hive 期望日期格式,yyyy-MM-dd
但在您的情况下,您要转换yyyyMMdd
为日期数据类型,这就是您在to_date之后获得空值的原因
hive> select concat(cast(cast(201809 as BIGINT) as STRING), "01");
+-----------+--+
| _c0 |
+-----------+--+
| 20180901 |
+-----------+--+
转换为日期:
hive> select to_date(from_unixtime(unix_timestamp(concat(bigint('201908'),'01'),
'yyyyMMdd'),'yyyy-MM-dd'));
+-------------+--+
| _c0 |
+-------------+--+
| 2019-08-01 |
+-------------+--+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句