string substr int 更新 cast 日期 hive dt

操作1:将int类型日期(如:20200114)转化为日期类型的日期(如:2020-01-14)
step1 创建表并写入数据
create table tb (dt INT);
insert into tb values (20180701);
insert into tb values (20180715);
step2 日期转化
select dt,
from_unixtime(unix_timestamp(cast(dt as string),‘yyyyMMdd’),‘yyyy-MM-dd’) as a,–方式1
concat(substr(cast(dt as string),1,4), ‘-’,substr(cast(dt as string),5,2), ‘-’,substr(cast(dt as string),7,2)) as b --方式2
from tb;
命令说明:
方式1:cast将int类型的dt转换为字符串,unix_timestamp将转化后的字符串转换为时间戳(这里需要自定义,yyyyMMdd必须对应前面cast转换后的字符串对应的日期格式),from_unixtime从时间戳中提取’yyyy-MM-dd’格式的日期
方式2:cast转换成字符串后进行拆分(substr)和拼接(concat)
操作2:日期增减操作
日期增加
date_add(string startdate, int days)
日期减少
date_sub (string startdate, int days)
注意,startdate需要转换成日期格式,一般是’yyyy-MM-dd’格式

tm.sk_date=20201231

from_unixtime(unix_timestamp(date_add(concat(substr(tm.sk_date,1,4),’-’,substr(tm.sk_date,5,2),’-’,substr(tm.sk_date,7,2)),cast(pnv.confirm_days - apt.delay_days as int))
,‘yyyy-mm-dd’),‘yyyymmdd’)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值