如何转换“2019-11-02T20:18:00Z"在 HQL 中添加时间戳? [英] How to convert "2019-11-02T20:18:00Z" to timestamp in HQL?

查看:16
本文介绍了如何转换“2019-11-02T20:18:00Z"在 HQL 中添加时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期时间字符串 "2019-11-02T20:18:00Z".如何在 Hive HQL 中将其转换为时间戳?

I have datetime string "2019-11-02T20:18:00Z". How can I convert it into timestamp in Hive HQL?

推荐答案

如果你想保留毫秒然后删除 Z,用空格替换 T 并转换为时间戳:

If you want preserve milliseconds then remove Z, replace T with space and convert to timestamp:

select timestamp(regexp_replace("2019-11-02T20:18:00Z", '^(.+?)T(.+?)Z$','$1 $2'));

结果:

2019-11-02 20:18:00

它也适用于毫秒:

 select timestamp(regexp_replace("2019-11-02T20:18:00.123Z", '^(.+?)T(.+?)Z$','$1 $2'));

结果:

2019-11-02 20:18:00.123

使用 from_unixtime(unix_timestamp()) 解决方案不适用于毫秒.演示:

Using from_unixtime(unix_timestamp()) solution does not work with milliseconds. Demo:

  select from_unixtime(unix_timestamp("2019-11-02T20:18:00.123Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));

结果:

2019-11-02 20:18:00

毫秒丢失.原因是 函数 unix_timestamp 返回秒 来自 UNIX 时代 (1970-01-01 00:00:00 UTC).

Milliseconds are lost. And the reason is that function unix_timestamp returns seconds passed from the UNIX epoch (1970-01-01 00:00:00 UTC).

这篇关于如何转换“2019-11-02T20:18:00Z"在 HQL 中添加时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆