拆分日期并将日期、月份和年份保存在不同的列中

vhipe2zx  于 2021-06-25  发布在  Hive
关注(0)|答案(3)|浏览(319)

输入

10-01-2019    

20-02-2019

22-03-2019

输出

Date     Month              Year 

10       January            2019    

20       February           2019

30       March              2019
nwlls2ji

nwlls2ji1#

试试这个

with t as ( select  unix_timestamp('10-01-2019' , 'dd-MM-yyyy') as dt )
select from_unixtime(dt,'dd')  as Date,
       from_unixtime(dt,'MMMM')  as Month,
       from_unixtime(dt,'YYYY')  as Year
 from t;

结果

Total MapReduce CPU Time Spent: 2 seconds 720 msec
OK
10  January 2019
Time taken: 23.206 seconds, Fetched: 1 row(s)
ee7vknir

ee7vknir2#

使用split():

with your_data as(
select stack(3,'10-01-2019',   
               '20-02-2019',
               '22-03-2019'
        ) as dt
) --use your table instead of this

select dt[0] as day,
       dt[1] as month,
       dt[2] as year
from ( select split(dt,'-') as dt from your_data )s;

结果:

OK
day     month   year
10      01      2019
20      02      2019
22      03      2019
Time taken: 0.081 seconds, Fetched: 3 row(s)
c86crjj0

c86crjj03#

我们需要使用 from_unixtime and unix_timestamp 函数来解析日期。
那么 split the fieldsubquery 提取日期,月份,年份。。 Example:hive> select dt[0] day,dt[1] month,dt[2] year from( select split(from_unixtime(unix_timestamp("10-01-2019",'dd-MM-yyyy'),'dd-MMMM-yyyy'),'-')dt )e;Result: ```
day month year
10 January 2019

相关问题