2023.12.3 hive-sql日期函数小练习

目录

时间函数练习 

时间戳

 周,季度等计算

 获取日期相关

获取当前时间


时间函数练习 


--日期函数练习  ,sub是英文subtraction减法的简写, add是英文addition加法的简写
--获取今天是本周的第几天
select dayofweek('2023-12-3');  --周日为一周的第一天
select current_timestamp;  --获取当前时间戳
select date_sub(current_timestamp(),1);-- 获取昨天的日期
select date_add(current_timestamp(),-1);-- 获取昨天的日期
-- 计算时间差
select datediff('2024-07-24','2023-07-24'); -- 新的时间在前,旧的在后 366
--计算现在离圣诞节,还有*天?
select datediff('2023-12-25',current_date); -- 22
select dayofmonth('2023-12-1');--获取12月1日是所在月的第几天,答案是第一天
select last_day('2023-12-1');--获取12月1日所在月的最后一天日期


select date_sub('2023-12-3',if(dayofweek('2023-12-3')= 1,7,dayofweek('2023-12-3')-2));
--判断12月3日是否一周的第一天(即周日)?
--如是,那么12月3日往前7天,得到11月26日

select date_sub('2023-12-10',if(dayofweek('2023-12-4')= 1,7,dayofweek('2023-12-10')-2));
--判断12月4日是否一周的第一天(即周日)?
--如不是,那么12月10日所处的星期数就减去2, 12月10日是一周的第1天,判断内算式1-2=-1,得到一个数为-1,
-- 之后date_sub减法函数再把12月10日减去-1天,负负得正变成+1天,最后得到了12月11号;

select date_add('2023-12-3',if(dayofweek('2023-12-3')=1,0,-dayofweek('2023-12-3')+8));
--判断12月3日是否是一周的第一天(即周日)?
--如是,则就取0,date_add加法函数就将2023-12-3 加上0天 ,最终就是12月3

select date_add('2023-12-4',if(dayofweek('2023-12-4')=1,0,-dayofweek('2023-12-4')+8));
--判断12月3日是否是一周的第一天(即周日)?
--如不是,就拿12月4日所在的星期数,12月4日是一周中的第二天,所以取2,算式结果为-2+8=6,
--最终应用date_add加法函数,2023-12-4加上6天,也就是日期往后6天,得到2023年12月10日

时间戳

-- 拓展
--获取当前UNIX时间戳函数: unix_timestamp
select unix_timestamp(); -- 1684640319
 
--字符串日期转UNIX时间戳函数: unix_timestamp
select unix_timestamp("2023-5-21 11:38:56"); -- 1684669136
 
--指定格式日期转UNIX时间戳函数: unix_timestamp
select unix_timestamp('20230521 11:38:56','yyyyMMdd HH:mm:ss'); --1684669136
 
--UNIX时间戳转日期函数: from_unixtime
select from_unixtime(1684669136); -- 2023-05-21 11:38:56
-- 获取时间原点日期
select from_unixtime(0); -- 1970-01-01 00:00:00

 周,季度等计算

-- 依次获取现在是月中第几天,周中第几天,季度,年中第几周
select dayofmonth(current_timestamp()); -- day of month 获取今天是本月中的第几天
select `dayofweek`(`current_timestamp`());-- day of week 获取今天是本周的第几天,星期天才是一周的开始
select quarter(`current_timestamp`()); -- quarter 获取现在是本年的第几个季度,一年分为四季
select weekofyear(`current_timestamp`()); -- week of year 获取本周是今年的第几周

 获取日期相关

-- 依次获取年月日时分秒
select year(`current_date`()); --2023
select month(`current_date`()); -- 11
select day(`current_date`());  --15
 
select hour(`current_timestamp`()); --11
select minute(`current_timestamp`()); --4
select second(`current_timestamp`()); --32
 
 
-- 计算时间差
select datediff('2024-07-24','2023-07-24'); -- 新的时间在前,旧的在后 366
 
-- 获取明天的日期
select date_add(current_timestamp(),1); --获取明天的日期
select date_sub(current_timestamp(),-1); --获取明天的日期
 
-- 获取昨天的日期
select date_sub(current_timestamp(),1);-- 获取昨天的日期
select date_add(current_timestamp(),-1);-- 获取昨天的日期

获取当前时间

-------------------------------------时间函数---------------------------------------------
-- 2.日期时间函数
-- 获取当前时间戳(时间原点到现在的秒/毫秒)
select unix_timestamp(); -- 1684639237
select current_timestamp(); -- 转换成现在的时间
 
-- 获取当前日期
select current_date(); -- 2023-05-21
 
-- 字符串格式时间戳转日期
select to_date('2023-05-21 11:19:31.222000000');
select to_date(current_timestamp());

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白白的wj

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值