Hive String to Timestamp conversion with Milliseconds

拥有回忆 提交于 2020-05-13 14:35:10

问题


I have a requirement to convert the mentioned input string format and produce the desired output in timestamp as shown below.

Input: 16AUG2001:23:46:32.876086

Desired Output: 2001-08-16 23:46:32.876086

Output which is coming by running the below code: 2001-08-17 00:01:08

Query:

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;

Milliseconds part is not getting converted as required. Please suggest.


回答1:


unix_timestamp function does not preserve milliseconds. Convert without milliseconds, then concatenate with millisecond part:

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;

Result:

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)


来源:https://stackoverflow.com/questions/59643063/hive-string-to-timestamp-conversion-with-milliseconds

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