怎么把“ 2019-11-02T20:18:00Z”转换成HQL中的时间戳?

时间:2019-11-05 07:31:55

标签: sql hive timestamp hiveql unix-timestamp

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

2 个答案:

答案 0 :(得分:2)

尝试一下:

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

答案 1 :(得分:1)

如果要保留毫秒数,请删除Z,将T替换为空格并转换为时间戳:

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())解决方案无法使用毫秒。 演示:

  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

毫秒丢失。原因是function unix_timestamp returns seconds passed from the UNIX epoch (1970-01-01 00:00:00 UTC)