How to convert a string to timestamp with milliseconds in Hive

前端 未结 5 1772
猫巷女王i
猫巷女王i 2021-02-04 11:27

I have a string \'20141014123456789\' which represents a timestamp with milliseconds that I need to convert to a timestamp in Hive (0.13.0) without losing the milliseconds.

5条回答
  •  我在风中等你
    2021-02-04 11:40

    A simple strategy would be to use date_format(arg1, arg2), where arg1 is the timestamp either as formatted string, date, or timestamp and the arg2 is the format of the string (in arg1). Refer to the SimpleDateFormat java documentation for what is acceptable in the format argument.

    So, in this case:

    date_format('20141014123456789', 'yyyyMMddHHmmssSSS')
    

    would yield the following string: '2014-10-14 12:34:56.789' which can then be cast as timestamp:

    cast(date_format('20141014123456789', 'yyyyMMddHHmmssSSS') as timestamp)
    

    The above statement would return timestamp (as desired).

提交回复
热议问题