In SQL 如何将时间转换成UNIX时间戳

In SQL How to convert time into UNIX timestamp

在蜂巢中有一些我有的数据。现在我想在秒内将 start_timestamp 转换为 unix_timestamp。怎么做?因为 start_timestamp 有两种格式:

第一种格式:

    2018-03-22 02:54:35

第二种格式:

    May 15 2018  5:15PM

第一个格式是 'yyyy-MM-dd HH:mm:ss',第二个是 'MMM dd yyyy hh:mm:aa'。如果格式错误,unix_timestamp函数将returnNULL。尝试使用一种格式转换,如果NULL,尝试使用另一种格式转换。这可以使用 coalesce 函数来完成:

select 
      coalesce(unix_timestamp(start_timestamp ,'yyyy-MM-dd HH:mm:ss'),
               unix_timestamp(start_timestamp ,'MMM dd yyyy hh:mm:aa')
              ) as UnixTimestamp
  from my_table;

如有必要,使用 from_unixtime() 将其转换回给定格式,如 .

在此处查看模式示例:SimpleDateFormat