How to change date format in hive?

前端 未结 4 1525
梦如初夏
梦如初夏 2020-12-10 12:26

My table in hive has a filed of date in the format of \'2016/06/01\'. but i find that it is not in harmory with the format of \'2016-06-01\'. They can not compare for insta

相关标签:
4条回答
  • 2020-12-10 12:58

    Use :

    unix_timestamp(DATE_COLUMN, string pattern)
    

    The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.

    Date Function

    0 讨论(0)
  • 2020-12-10 13:12

    To convert date string from one format to another you have to use two date function of hive

    1. unix_timestamp(string date, string pattern) convert time string with given pattern to unix time stamp (in seconds), return 0 if fail.
    2. from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone.

    Using above two function you can achieve your desired result.

    The sample input and output can be seen from below image:

    The final query is

    select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1; 
    

    where table1 is the table name present in my hive database.

    I hope this help you!!!

    0 讨论(0)
  • 2020-12-10 13:15
    cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
    
    0 讨论(0)
  • 2020-12-10 13:17

    Let's say you have a column 'birth_day' in your table which is in your format, you should use the following query to convert birth_day into the required format.

    date_Format(birth_day, 'yyyy-MM-dd')
    

    You can use it in a query in the following way

    select * from yourtable
    where 
    date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
    
    0 讨论(0)
提交回复
热议问题