拆分日期并将日期、月份、年份保存在不同的列中
Posted
技术标签:
【中文标题】拆分日期并将日期、月份、年份保存在不同的列中【英文标题】:Split the date and save the date, month, year in different columns 【发布时间】:2019-10-18 12:57:17 【问题描述】:输入
10-01-2019
20-02-2019
22-03-2019
输出
Date Month Year
10 January 2019
20 February 2019
30 March 2019
【问题讨论】:
【参考方案1】:使用拆分():
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)
【讨论】:
【参考方案2】:我们需要使用from_unixtime and unix_timestamp
函数来解析日期。
然后split the field
in 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
【讨论】:
【参考方案3】:试试这个
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)
【讨论】:
以上是关于拆分日期并将日期、月份、年份保存在不同的列中的主要内容,如果未能解决你的问题,请参考以下文章