且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

配置单元:转换";yyyy-MM-dd';T';HH:mm:ss.SSS';Z';";中缺少秒的字符串日期时间

更新时间:2022-11-30 12:36:38

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