如何将“ 2019-11-02T20:18:00Z”转换为HQL中的时间戳?

问题描述 投票:1回答:2

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

sql hive timestamp hiveql unix-timestamp
2个回答
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)


2
投票

尝试一下:

select from_unixtime(unix_timestamp("2019-11-02T20:18:00Z", "yyyy-MM-dd'T'HH:mm:ss"))
© www.soinside.com 2019 - 2024. All rights reserved.