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());
相关推荐
ss2732 小时前
PostgreSQL表级锁、行级锁、轻量级锁
数据库·postgresql·区块链
RacentCLM2 小时前
证书即服务CaaS:从申请、续期到部署的自动化链路拆解
运维·数据库·自动化·ssl
SelectDB3 小时前
安全评审卡了三周,我把部署形态的四种选项重新排了一遍
大数据·数据库·云原生
注塑机数据采集联网3 小时前
注塑机生产管控MES系统应用
大数据·运维·服务器·人工智能·物联网
Random Glow5 小时前
二级索引与回表:一次回表到底多贵
数据库·mysql·性能优化·innodb·索引
CarIise5 小时前
JDBC 与数据库事务核心
数据库·笔记
见闻小天地5 小时前
新手入门:印刷包装设备的伺服和PLC运动控制方案各管什么?七步选型讲清楚
运维·网络·数据库·人工智能
hyunbar7775 小时前
华为云 GaussDB(DWS) 和 PG 的谓词下推有何差异
sql
海鸥-w5 小时前
Redis 配置与排错说明
java·数据库·redis·bootstrap