2017-04-23 21 views
2

我需要從配置單元的字符串列中提取日期和小時。Hive查詢從字符串中分別提取日期和小時

表: enter image description here

select TO_DATE(from_unixtime(UNIX_TIMESTAMP(dates,'dd/MM/yyyy'))) from dates; 

output: 

0016-01-01 
0016-01-01 

select TO_DATE(from_unixtime(UNIX_TIMESTAMP(dates,'hh'))) from dates; 

output: 

1970-01-01 
1970-01-01 

請告知如何從表列取日期seperately和小時seperately。

+0

(1)去除不相關的標籤(2)替換文字圖片(3)有許多不良的方式來存儲日期/時間戳,但這是最糟糕的之一 –

回答

3

我有數據樣本更改爲更具合理

with dates as (select explode(array('1/11/16 3:29','12/7/16 17:19')) as dates) 

select from_unixtime(unix_timestamp(dates,'dd/MM/yy HH:mm'),'yyyy-MM-dd') as the_date 
     ,from_unixtime(unix_timestamp(dates,'dd/MM/yy HH:mm'),'H')   as H 
     ,from_unixtime(unix_timestamp(dates,'dd/MM/yy HH:mm'),'HH')   as HH 

from dates 

+------------+----+----+ 
| the_date | h | hh | 
+------------+----+----+ 
| 2016-11-01 | 3 | 03 | 
| 2016-07-12 | 17 | 17 | 
+------------+----+----+