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());
相关推荐
nice_lcj5202 小时前
MySQL 主从复制与读写分离:从库怎么跟着主库走
数据库·mysql·面试·binlog·读写分离·主从复制
tianyuanwo3 小时前
PostgreSQL 部署运维实战总结:从安装踩坑到生产可用
运维·数据库·postgresql
DataY轻量数据平台3 小时前
任务调度的三种模式:什么时候跑、按什么顺序跑、等谁跑完
数据仓库·etl
数据工匠老o3 小时前
数据库没死,业务卡住了:行锁等待、元数据锁、长事务的排查路径
数据库·算法·架构
Decay__3 小时前
SQLServer 优化案例集 —— 慢 SQL 优化、索引重构与参数调整收益对比
运维·数据库·sql·mysql·架构
大大大大晴天4 小时前
每天认识一个组件:VictoriaMetrics
大数据
CoderYanger4 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第二弹)
java·数据库·面试·职场和发展·java-ee·tomcat·mybatis
龙腾AI白云5 小时前
具身智能的空间导航与未知环境探索
大数据·数据库·人工智能·机器学习·知识图谱
ClouGence5 小时前
DBeaver太重,Navicat付费?试试这4款好用的开源数据库管理工具!
数据库·开源