我需要转换上述输入字符串格式并以时间戳形式生成所需的输出,如下所示。
输入: 16AUG2001:23:46:32.876086
所需输出: 2001-08-16 23:46:32.876086
运行以下代码得出的输出:2001-08-17 00:01:08
查询:
select '16AUG2001:23:46:32.876086' as row_ins_timestamp,
from_unixtime(unix_timestamp('16AUG2001:23:46:32.876086',
'ddMMMyyyy:HH:mm:ss.SSSSSS')) as row_ins_timestamp
from temp;
毫秒部分未按要求进行转换。请提出建议。
最佳答案
unix_timestamp 函数不保留毫秒。 不带毫秒进行转换,然后与毫秒部分连接:
with your_data as (
select stack(3,
'16AUG2001:23:46:32.876086',
'16AUG2001:23:46:32',
'16AUG2001:23:46:32.123'
) as ts
)
select concat_ws('.',from_unixtime(unix_timestamp(split(ts,'\\.')[0],'ddMMMyyyy:HH:mm:ss')),split(ts,'\\.')[1])
from your_data;
结果:
2001-08-16 23:46:32.876086
2001-08-16 23:46:32
2001-08-16 23:46:32.123
Time taken: 0.089 seconds, Fetched: 3 row(s)
关于sql - Hive 字符串到时间戳的转换(以毫秒为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59643063/