分割日期并将日期,月份,年份保存在不同的列中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分割日期并将日期,月份,年份保存在不同的列中相关的知识,希望对你有一定的参考价值。
输入
10-01-2019
20-02-2019
22-03-2019
输出
Date Month Year
10 January 2019
20 February 2019
30 March 2019
答案
尝试一下
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)
另一答案
我们需要使用from_unixtime and unix_timestamp
函数来解析日期。
然后在split the field
中选择subquery
并提取日期,月份,年份。
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
另一答案
with your_data as(
select stack(3,'10-01-2019',
'20-02-2019',
'22-03-2019'
) as dt
)
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)
以上是关于分割日期并将日期,月份,年份保存在不同的列中的主要内容,如果未能解决你的问题,请参考以下文章