HiveQL转换或同步以不同格式存储的日期类型

时间:2017-08-05 14:48:00

标签: datetime hive hiveql

我正在尝试在不同的表中同步两个时间指示符列以准备连接。问题是date1中的db1是字符串格式,没有尾随零,date2中的db2是时间戳格式,但是对于没有小时数的日期有尾随零或纳秒。我在下面显示变量。我如何同步这两个?到目前为止,我已经尝试了一些建议,但没有成功。例如,当我对cast使用as timestamp date1转换时,对于没有时间信息的日期,我得到null。我从下面的表格列中显示了样本数据。

date1 (string)

2017-05-13 11:46:12
2017-06-17
2017-05-19 05:34:52.78
2017-06-16 12:10:13.177
2017-05-25 05:32:05.99
2017-05-25 06:43:35.007
2017-05-13 11:45:47.873
2017-06-05

date2 (timestamp)

7/18/2013 18:08:48.000000
8/26/2015 00:00:00.000000
5/7/2015 20:03:25.000000
8/16/2014 12:08:48.000000
3/17/2017 11:05:52.530000
1/9/2014 21:11:49.000000
6/16/2016 14:22:40.157000
5/5/2017 14:12:48.497000

1 个答案:

答案 0 :(得分:1)

日期1

with    t as 
        (
            select  explode
                    (
                        array
                        (
                            '2017-05-13 11:46:12'
                           ,'2017-06-17'
                           ,'2017-05-19 05:34:52.78'
                           ,'2017-06-16 12:10:13.177'
                           ,'2017-05-25 05:32:05.99'
                           ,'2017-05-25 06:43:35.007'
                           ,'2017-05-13 11:45:47.873'
                           ,'2017-06-05'
                        )
                    )   as date1
        )
select  cast(date1 as timestamp)
from    t
;
2017-05-13 11:46:12
2017-06-17 00:00:00
2017-05-19 05:34:52.78
2017-06-16 12:10:13.177
2017-05-25 05:32:05.99
2017-05-25 06:43:35.007
2017-05-13 11:45:47.873
2017-06-05 00:00:00

date2

with    t as 
        (
            select  explode
                    (
                        array
                        (
                            '7/18/2013 18:08:48.000000'
                           ,'8/26/2015 00:00:00.000000'
                           ,'5/7/2015 20:03:25.000000'
                           ,'8/16/2014 12:08:48.000000'
                           ,'3/17/2017 11:05:52.530000'
                           ,'1/9/2014 21:11:49.000000'
                           ,'6/16/2016 14:22:40.157000'
                        )
                    )   as date2
        )
select  cast(printf('%04d-%02d-%02d %s',int(d2[2]),int(d2[0]),int(d2[1]),d2[3]) as timestamp)
from    (select split(date2,'[/ ]') as d2 from t) t
;
2013-07-18 18:08:48
2015-08-26 00:00:00
2015-05-07 20:03:25
2014-08-16 12:08:48
2017-03-17 11:05:52.53
2014-01-09 21:11:49
2016-06-16 14:22:40.157