hive实现获取当前季度第一天

在Hive的低版本中,如果需要获取当前季度的第一天,可以通过多步计算实现。首先,利用`floor(substr(current_timestamp,6,2)/3.1)*3)+1`计算出本季度的月份,然后根据月份拼接年份和‘01’来得到季度第一天的日期字符串。对于12月的情况,也会正确转化为下个季度的第一天。最后,通过`from_unixtime(unix_timestamp(date,'yyyyMMdd'))`将字符串转换为标准日期格式。

hive3中现在支持了
select trunc(‘2021-03-01’,‘Q’)
即可获得当前季度第一天,但是低版本的还是要自己实现。

首先可以通过

 (floor(substr(current_timestamp,6,2)/3.1)*3)+1       --获取的是本季度的月份

例:select current_timestamp,(floor(substr(current_timestamp,6,2)/3.1)*3)+1
输出:
     2020-09-29 22:34:25.533       7

然后通过拼接的方式实现,注意季度月份为1,4,7,10,拼接时需要进行判断。

select concat(year(current_timestamp),
          case when (floor(substr(current_timestamp,6,2)/3.1)*3)+1<10 then concat(0,(floor(substr(current_timestamp,6,2)/3.1)*3)+1)
          else (floor(substr(current_timestamp,6,2)/3.1)*3)+1 end,'01')
输出结果:2020070112月份某日期进行测试:
select concat(year('2020-12-12'),
          case when (floor(substr('2020-12-12',6,2)/3.1)*3)+1<10 then concat(0,(floor(substr('2020-12-12',6,2)/3.1)*3)+1)
          else (floor(substr('2020-12-12',6,2)/3.1)*3)+1 end,'01')
输出:20201001

注:最后输出结果为string类型使用from_unixtime(unix_timestamp(date,'yyyyMMdd'))可以转换为标准日期格式
例:
select from_unixtime(unix_timestamp(concat(year('2020-12-12'),
          case when (floor(substr('2020-12-12',6,2)/3.1)*3)+1<10 then concat(0,(floor(substr('2020-12-12',6,2)/3.1)*3)+1)
          else (floor(substr('2020-12-12',6,2)/3.1)*3)+1 end,'01'),'yyyyMMdd')
 输出结果:2020-10-01 00:00:00
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

¤睿

皆是缘

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值