网站首页 > 知识剖析 正文
1、获取当前时间
select getdate();
2、获取昨天(字符串格式)
select to_char(dateadd(getdate(),-1,'dd'),'yyyymmdd');
select ${bdp.system.bizdate};
3、时间做加减
函数:dateadd(),加减量可以是年月日小时分钟秒,例如获取当前时间上月、7天后当前时间
select dateadd(getdate(),-1,'mm');
select dateadd(getdate(),7,'dd');
4、获取上月初及上月最后一天(可类推到本月下月等等)
select concat(to_char(dateadd(getdate(),-1,'mm'),'yyyy-mm'),'-01');
select last_day(add_months(FROM_UNIXTIME(UNIX_TIMESTAMP()),-1));
5、截取时间中的特定值
select to_char(getdate(),'yyyymmddhhmiss');
年(yyyy)
月(mm)
天(dd)
小时(hh)
分钟(mi)
秒(ss)
6、字符串转日期
select to_date('20230320','yyyymmdd');
7、时间戳日期相互转化
select FROM_UNIXTIME(cast('1674048165' as bigint));
--如果是毫秒(13位)的话,需要除以1000转化为秒级,时间戳转时间时格式得为bigint
select unix_timestamp(getdate());
8、获取当前时间的年周及星期
select concat(to_char(getdate(),'yyyy'),'年',weekofyear(getdate()),'周');
select WEEKDAY(getdate());
9、获取时间差
select datediff(dateadd(getdate(),7,'dd'),getdate(),'dd');
--最后一个参数控制时间差地单位,可以是年月日小时分钟秒
10、判断一个日期字符串能否根据指定的格式串转换为一个日期值
select isdate('2023-03-20 18:00:52','yyyy-mm-dd hh:mi:ss');
--如果能转换成功,返回True;否则返回False。
11、获取各个日期单位的初始值
datetrunc()
select datetrunc(getdate(),'yyyy');--当前时间年初
select datetrunc(getdate(),'mm');--当前时间月初
select datetrunc(getdate(),'dd');--当前时间日初
12、返回大于当前日期并且与week相匹配的第一个日期,即下周几的具体日期。
select next_day(getdate(),'sat');
--第二个参数为星期的前两个或三个字母
- 上一篇: MySQL常用函数(MySQL常用函数案例)
- 下一篇: mysql——日期操作(mysql日期+1)
猜你喜欢
- 2025-06-23 MySQL 8.0 参考手册— 内置函数和运算符参考
- 2025-06-23 SqlServer根据身份证号码查询出生日期和年龄
- 2025-06-23 VBA数组处理数据的财会实战案例(vba数组计算函数)
- 2025-06-23 mysql——日期操作(mysql日期+1)
- 2025-06-23 MySQL常用函数(MySQL常用函数案例)
- 2025-06-23 大数据量惯用优化方法(大数据量解决方案)
- 2025-06-23 一文掌握 DuckDB 时间序列分析:窗口函数实战详解
- 2025-06-23 MySQL 时间计算组合:助力精准工龄及补偿金计算
- 2025-06-23 SELECT list is not in GROUP BY clause_mysql执行的时候报错了
- 2025-06-23 使用python写一个简单的到期事件钉钉提醒功能
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)