2023.12.3 hive-sql日期函数小练习

目录

时间函数练习

时间戳

周,季度等计算

获取日期相关

获取当前时间


时间函数练习

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日

时间戳

sql 复制代码
-- 拓展
--获取当前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

周,季度等计算

sql 复制代码
-- 依次获取现在是月中第几天,周中第几天,季度,年中第几周
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 获取本周是今年的第几周

获取日期相关

sql 复制代码
-- 依次获取年月日时分秒
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);-- 获取昨天的日期

获取当前时间

sql 复制代码
-------------------------------------时间函数---------------------------------------------
-- 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());
相关推荐
QD.Joker24 分钟前
Django ORM 单表操作
数据库·django
chat2tomorrow41 分钟前
如何构建类似云数据仓库 Snowflake 的本地数据仓库?
大数据·数据仓库·低代码·数据治理·snowflake·sql2api
Linux运维老纪41 分钟前
Linux之 grep、find、ls、wc 命令
linux·运维·服务器·数据库·云计算·运维开发
焱焱枫1 小时前
Oracle 19c部署之数据库软件安装(二)
数据库·oracle
一代...1 小时前
【Redis】Redis基本命令(1)
数据库·redis·缓存
孟意昶1 小时前
大数据面试问答-HBase/ClickHouse
大数据·面试·hbase
八股文领域大手子2 小时前
深入浅出 Redis:核心数据结构解析与应用场景Redis 数据结构
java·数据结构·数据库·人工智能·spring boot·redis·后端
atbigapp.com2 小时前
DeepSeek在数据仓库的10大应用场景
大数据·数据库·人工智能
一只专注api接口开发的技术猿2 小时前
基于 Java 的淘宝 API 调用实践:商品详情页 JSON 数据结构解析与重构
大数据·数据结构·重构·json
结衣结衣.2 小时前
【MySQL】库的操作
linux·数据库·mysql