Search code examples
hivetimestamphql

hive - Get date in mm/dd/yyyy format


Using Hive, I have date dates in yyyyMMdd format and I need it in 'MM/dd/yyyy' format.

SELECT dt, 
    CAST(SUBSTRING(FROM_UNIXTIME(UNIX_TIMESTAMP(dt, 'MMddyyyy')), 1, 10) AS date) 
FROM timetable

Solution

  • No need for cast and substring.Specify the dateformat for dt in unix_timestamp() and the desired dateformat for from_unixtime()

    select 
           dt,
           from_unixtime(unix_timestamp(dt,'yyyyMMdd'),'MM/dd/yyyy') 
    from timetable;