当前位置:网站首页>sqlserver编写通用脚本实现获取一年前日期的方法
sqlserver编写通用脚本实现获取一年前日期的方法
2022-08-05 10:29:00 【bingbangx】
问题:
在数据库编程开发中,有时需要获取一年前的日期,以便以此为时间的分界点,查询其前后对应的数据量。例如:
1. 想查询截止到一年前当天0点之前的数据量,以及一年前当天0点开始到现在的数据量。
2. 想查询截止到一年前当天24点之前的数据量,以及一年前当天24点开始到现在的数据量。
3. 想查询截止到一年前当月1日0点之前的数据量,以及一年前当月1日0点开始到现在的数据量。
4. 想查询截止到一年前当月最后一天24点之前的数据量,以及一年前当月最后一天24点开始到现在的数据量。
以上这四种情况的具体查询场景,有archive数据脚本开发之前对数据库数据量的分布情况进行统计等,也有根据当前日期动态计算一年来的增量增幅情况等。
如果每次接到这样的需求需要获取一年前日期,然后每次重新思考怎么去实现肯定是不明智的,或者说公司内已经有人写了脚本实现,但其他同事遇到相同的问题,又重新构思一次,这样就比较浪费时间和精力,耗在这个细节上一些精力,有点像要做饭可是没有米就要先去买米的感觉,而且容易造成不同人写的脚本不统一,不利于代码规范化标准化的原则,不同的人跟进的时候还要去想想之前的人写的是什么逻辑。而且重新开发脚本的话,又需要重新进行自测,不利于提高工作效率。
解决方案:
首先简单介绍一下基本的知识点:
getdate()是获取当前日期;
dateadd可以对日期进行增减,在这里用来对年份减少1【dateadd(year, -1, 日期字符串)】,也可以用来对月份增加1【dateadd(month, 1, 日期字符串)】;
convert可以对日期进行字符串截取转换操作,在这里可以只截取形如2019-07-17的年月日【convert(varchar(10), 日期字符串, 120))】,也可以只截取形如2019-07的年月部分【convert(varchar(7), 日期字符串, 120))】。
然后就是针对上面4个问题对应的4个解决方法:
1. 最简单,对当前日期进行减少1年的运算,然后只截取年月日。
2. 先对当前日期进行增加1天的运算,然后再减少1年,最后只截取年月日。
3. 先对当前日期进行截取年月操作,然后再指定为当月01日,再减少1年,最后只截取年月日。
4. 先对当前日期进行截取年月操作,然后再指定为当月01日,再减少1年,并加上1个月,最后只截取年月日。
最后就可以直接在查询脚本条件中使用这个时间节点:
查询统计时间节点之前:select count(*) from 表 where 时间字段 < @datePoint
查询统计时间节点开始到现在:select count(*) from 表 where 时间字段 >= @datePoint
脚本:
/* 功能:获取一年前日期 作者:zhang502219048 脚本来源:https://www.cnblogs.com/zhang502219048/p/11198789.html */ --1.截止到一年前当天0点 declare @datePoint datetime = convert(varchar(10), dateadd(year, -1, getdate()), 120) select @datePoint as DatePoint go --2.截止到一年前当天24点 declare @datePoint datetime = convert(varchar(10), dateadd(year, -1, dateadd(day, 1, getdate())), 120) select @datePoint as DatePoint go --3.截止到一年前当月1日0点 declare @datePoint datetime = convert(varchar(10), dateadd(year, -1, convert(varchar(7), getdate(), 120) + '-01'), 120) select @datePoint as DatePoint go --4.截止到一年前当月最后一天24点 declare @datePoint datetime = convert(varchar(10), dateadd(month, 1, dateadd(year, -1, convert(varchar(7), getdate(), 120) + '-01')), 120) select @datePoint as DatePoint go
脚本运行结果:

总结
以上所述是小编给大家介绍的sql server编写通用脚本实现获取一年前日期的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
边栏推荐
猜你喜欢

电气工程的标准是什么

Our Web3 Entrepreneurship Project, Yellow

字节一面:TCP 和 UDP 可以使用同一个端口吗?

2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享

three objects are arranged in a spherical shape around the circumference

多线程(进阶) - 2.5w字总结

The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy

E-sports, convenience, efficiency, security, key words for OriginOS functions

JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number

Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
随机推荐
Header file search rules when compiling with GCC
创建一个 Dapp,为什么要选择波卡?
多线程(进阶) - 2.5w字总结
上位机开发C#语言:模拟STC串口助手接收单片机发送数据
Go compilation principle series 6 (type checking)
FPGA: Basic Getting Started LED Lights Blinking
js hijacks the array push method
Import Excel/CSV from Sub Grid within Dynamics 365
Data Middle Office Construction (10): Data Security Management
第四章:redis 数组结构的set和一些通用命令「建议收藏」
nyoj754 黑心医生 结构体优先队列
How to choose coins and determine the corresponding strategy research
19.3 restart the Oracle environment
60行从零开始自己动手写FutureTask是什么体验?
The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
Login function and logout function (St. Regis Takeaway)
012_SSS_ Improving Diffusion Model Efficiency Through Patching
[强网杯2022]WP-UM