配置单元:转换";yyyy-MM-dd';T';HH:mm:ss.SSS';Z';";中缺少秒的字符串日期时间 [英] Hive: Convert string datetime with missing seconds in "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
本文介绍了配置单元:转换";yyyy-MM-dd';T';HH:mm:ss.SSS';Z';";中缺少秒的字符串日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码将字符串DATETIME变量转换为DATETIME,但转换后的字符串缺少SSS部分。
使用的代码:
cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime
结果:
2019-03-08T18:28:36.901Z转换为08MAR2019:18:28:36.000000
字符串中的其他一些旧时间:
2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z
转换的时间中缺少SSS部件"901"。我需要有关保留SSS部件的帮助,因为我需要按记录的确切时间对其进行排序。
谢谢!
推荐答案
from_unixtime
始终是分钟(yyyy-MM-dd HH:mm:ss)
要获取millisecs
,我们需要执行一些解决办法。
- 我们将使用
regexp_extract
从old_time中提取millisecs
,然后concat
提取到from_unixtime
结果,最后转换为timestamp
。
Example:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e
+------------------------+-----------------------+
|old_time |newtime |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+
UPDATE:
您的示例数据在:
毫秒之前,请尝试使用以下查询:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41:335Z")old_time)e
这篇关于配置单元:转换";yyyy-MM-dd';T';HH:mm:ss.SSS';Z';";中缺少秒的字符串日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文