HIVE 求解时间差 & 日期格式转换

  • PART1  时间差

 

阅读:https://www.jianshu.com/p/d01d095edc4a

 

1 将时间转换成时间戳

2 将时间戳相减

3 注意单位(分钟、毫秒)

E.x.   CAST((unix_timestamp(rt.refund_ticket_time) - unix_timestamp(rt.create_time)) / 60 AS int)<=30
 

 

  • PART 2 日期格式转换

阅读:https://blog.csdn.net/weixin_37536446/article/details/82419601

 

在解析埋点数据时会遇到两种不同的日期格式:yyyymmdd和yyyy-mm-dd,此类型之间的转换主要有两种思路:

  • 第一种方法:from_unixtime+unix_timestamp

--20180905转成2018-09-05
select from_unixtime(unix_timestamp('20180905','yyyymmdd'),'yyyy-mm-dd')
from dw.ceshi_data
--结果如下:
2018-09-05
 
--2018-09-05转成20180905
select from_unixtime(unix_timestamp('2018-09-05','yyyy-mm-dd'),'yyyymmdd')
from dw.ceshi_data
--结果如下:
20180905

  • 第二种方法:substr + concat

--20180905转成2018-09-05 
select concat(substr('20180905',1,4),'-',substr('20180905',5,2),'-',substr('20180905',7,2)) from dw.ceshi_data
结果如下:
2018-09-05 
 
--2018-09-05转成20180905
select concat(substr('2018-09-05',1,4),substr('2018-09-05',6,2),substr('2018-09-05',9,2)) from dw.ceshi_data
结果如下:
20180905

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值