当前位置:网站首页>sql语句之根据起始结束日期获取每一天、周、月、年
sql语句之根据起始结束日期获取每一天、周、月、年
2022-04-22 19:22:00 【平山CP3】
背景:很多的统计报表中需要输入起始,结束日期来按照不同的时间单位查询数据,而且显示出来的列名是你的动态时间单位。你的表数据是按天存的日期,那么这时候你可能要做一些sql函数或者存储过程来实现了。
1.根据起始和结束日期来获取范围中的每一天,作为结果集返回(函数)
create function [dbo].[fn_GetDateRange](@DATE_START datetime, @DATE_END datetime)
RETURNS TABLE
AS
RETURN
(
SELECT convert(char(10), DATEADD(dd, number, @DATE_START),120) AS DATA_DATE
FROM master.dbo.spt_values as spt
WHERE type = 'p' AND number <= DATEDIFF(DAY, @DATE_START, @DATE_END)
)
GO
select * from fn_GetDateRange('2018-07-31','2018-08-10')
结果展示:

2.根据起始和结束日期来获取范围中的每周(包括当周起始和结束日期),作为结果集返回(存储过程)
CREATE PROCEDURE [dbo].[GetWeekRange]
@tmpDate datetime, @DATE_END datetime
AS
BEGIN
SET DATEFIRST 1
DECLARE @index INT
DECLARE @table TABLE
(
WeekTimes INT ,
FirstDay datetime ,
EndDay datetime
)
DECLARE @YearFistWK INT
SET @YearFistWK= DATEPART(dw, @tmpDate)
SET @index = ( SELECT DATEPART(WEEK, @tmpDate))
IF ( @YearFistWK = 7 )
BEGIN
SET @index = @index + 1
END
WHILE @tmpDate <= @DATE_END
BEGIN
INSERT INTO @table
SELECT @index ,
a.FirstDay ,
b.EndDay
FROM ( SELECT 1 AS ID ,
DATEADD(wk, DATEDIFF(wk, 0, @tmpDate), 0) AS FirstDAy
) a
LEFT JOIN ( SELECT 1 AS ID ,
DATEADD(wk,
DATEDIFF(wk, 0, @tmpDate),
6) AS EndDay
) b ON a.ID = b.ID
SET @tmpDate = DATEADD(DAY, 7, @tmpDate)
SET @index = @index + 1
END
SELECT WeekTimes, CONVERT(varchar(100), FirstDay, 23) FirstDay,CONVERT(varchar(100), EndDay, 23) EndDay
FROM @table
END
GO
exec GetWeekRange '2018-07-31','2018-08-10'
结果展示:

3.根据起始和结束日期来获取范围中的每月,作为结果集返回(存储过程)
CREATE PROCEDURE [dbo].[GetMonthRange]
@tmpDate datetime, @DATE_END datetime
AS
BEGIN
DECLARE @table TABLE
(
DateMonth datetime
)
WHILE @tmpDate <= @DATE_END
BEGIN
INSERT INTO @table
SELECT DATEADD(mm, DATEDIFF(mm, 0, @tmpDate), 0) AS DateMonth
SET @tmpDate = CONVERT(varchar(7), DATEADD(mm, 1, @tmpDate) , 120) + '-01'
END
select CONVERT(varchar(7), DateMonth, 23) DateMonth from @table
END
GO
exec GetMonthRange '2018-07-31','2018-08-10'
结果展示:

4.根据起始和结束日期来获取范围中的每年,作为结果集返回(存储过程)
CREATE PROCEDURE [dbo].[GetYearRange]
@tmpDate datetime, @DATE_END datetime
AS
BEGIN
DECLARE @table TABLE
(
DateYear datetime
)
WHILE @tmpDate <= @DATE_END
BEGIN
INSERT INTO @table
SELECT DATEADD(yy, DATEDIFF(yy, 0, @tmpDate), 0) AS DateYear
SET @tmpDate = CONVERT(varchar(4), DATEADD(yy, 1, @tmpDate) , 120) + '-01-01'
END
select CONVERT(varchar(4), DateYear, 23) DateYear from @table
END
GO
exec GetYearRange '2017-08-31','2018-08-10'
结果展示:

应该还要好的方法。望各位大佬不吝赐教
版权声明
本文为[平山CP3]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_33277597/article/details/82215827
边栏推荐
- uniapp商品分类选项卡
- 时间日期格式化
- 09-Partitioning
- Revit (5) - second opening - creating walls
- . net core minimal API upload file
- 2路CAN/CAN FD 数据记录诊断仪为企业解决偶发性错误难点
- WebRTC:Mesh/MCU/SFU三种通信架构
- Detailed network structure diagram of dbnet character detection
- 值得推荐的chrome插件
- Originally, this is the correct posture for developers to open world book day
猜你喜欢

Project training - Design and development of 2D multiplayer fighting game based on unity (v. use audiomixer to control the volume)

Project training - Design and development of 2D multiplayer scuffle game based on unity (III. unity plasticscm multiplayer collaboration)

linux系统下redis相关问题

完美转发实现机制

mmocr DBLoss

Detailed explanation of transfer method

LeetCode 41. Missing first positive number

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video

LeetCode 41. 缺失的第一个正数

mmocr DBLoss
随机推荐
项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(五、利用AudioMixer控制音量大小)
final的作用以及String为什么不可变
Markdown 学习和实践
对《继续树莓派4B+OLED:开机自动显示IP地址》的补充,针对 Raspberry Pi OS Lite 64-bit
【Appium踩坑】Failed to capture a screenshot. Does the current view have ‘secure‘ flag set?
Project training - Design and development of 2D multiplayer fighting game based on unity (v. use audiomixer to control the volume)
uniapp商品分类选项卡
if-else 优化
ArrayList learning notes
10-Streaming Query
mmocr DBLoss
防火门可以根据EN 1634-1 标准测试吗?
[appium stepping on the pit] could not proxy command to the remote server Original error: timeout of 240000ms exceeded
消息队列最佳实践
LeetCode 41. Missing first positive number
Can fire doors be tested according to en 1634-1?
13-Set Time Zone
Incorrect string value: ‘\xF0\x9F\x92\x95\’
Redis的key和value最佳实践
LC刷题第四天