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

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
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值