How to convert “2019-11-02T20:18:00Z” to timestamp in HQL?

拟墨画扇 提交于 2021-02-15 07:28:06

问题


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


回答1:


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'));

Result:

2019-11-02 20:18:00

Also it works with milliseconds:

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

Result:

2019-11-02 20:18:00.123

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'"));

Result:

2019-11-02 20:18:00

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).




回答2:


try this:

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


来源:https://stackoverflow.com/questions/58706470/how-to-convert-2019-11-02t201800z-to-timestamp-in-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!