【问题标题】:In SQL How to convert time into UNIX timestamp在 SQL 中如何将时间转换为 UNIX 时间戳
【发布时间】:2018-11-02 15:49:27
【问题描述】:
在 hive 中,我有一些数据。现在我想在第二个将start_timestamp 转换为unix_timestamp。怎么做?因为start_timestamp有两种格式:
第一种格式:
2018-03-22 02:54:35
第二种格式:
May 15 2018 5:15PM
【问题讨论】:
标签:
sql
hive
hiveql
unix-timestamp
【解决方案1】:
第一种格式是'yyyy-MM-dd HH:mm:ss',第二种是'MMM dd yyyy hh:mm:aa'。如果格式错误,unix_timestamp 函数将返回NULL。尝试使用一种格式转换,如果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() 将其转换回给定格式,例如answer。
在此处查看模式示例:SimpleDateFormat