当前位置:网站首页>The SQL statement obtains each day, week, month and year according to the start and end date
The SQL statement obtains each day, week, month and year according to the start and end date
2022-04-22 19:23:00 【Pingshan CP3】
background : In many statistical reports, you need to enter the starting point , End date to query data according to different time units , And the column name displayed is your dynamic time unit . Your table data is saved by day , Then you may have to do something at this time sql Function or stored procedure .
1. Get each day in the range according to the start and end dates , Return as a result set ( function )
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')
Result display :

2. Gets the week in the range based on the start and end dates ( Include the start and end dates of the week ), Return as a result set ( stored procedure )
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'
Result display :

3. Get the month in the range according to the start and end dates , Return as a result set ( stored procedure )
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'
Result display :

4. Get the year in the range according to the start and end dates , Return as a result set ( stored procedure )
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'
Result display :

There should be better ways . I hope you guys will give me some advice
版权声明
本文为[Pingshan CP3]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221922242268.html
边栏推荐
- (3) MySQL constraints
- leetcode:642.设计搜索自动补全系统
- 13-Set Time Zone
- 13-Set Time Zone
- JWT token 实践问题解决
- 关于字符串常量池,intern方法的理解
- 7.数据中台 --- 数据开发:数据体系建设
- 12-Delta Lake
- Project training - Design and development of 2D multiplayer fighting game based on unity (v. use audiomixer to control the volume)
- 项目实训- 基于unity的2D多人乱斗闯关游戏设计与开发(五、利用AudioMixer控制音量大小)
猜你喜欢

EXCEL 多个相似表格合并计算(合并计算工具)

14-Sprak设置自动分区

MySQL query with serial number

防火门能申请BS 476-21 耐火测试吗?

if-else 优化

WebRTC:Mesh/MCU/SFU三种通信架构

leetcode:642. Design search automatic completion system

(3) MySQL constraints

RoyalScope快速定位CAN网络中的故障节点,排产CAN总线质量

调用mysql存储过程报错:mysql 1449 : The user specified as a definer ('root'@'%') does not exist
随机推荐
13-Set Time Zone
第三次作业解析
.net 后台上传图片不用保存图片实现压缩图片
MYSQL,组合的唯一索引中,处理NULL值的问题
7. Data Center - Data Development: Data System Construction
Webrtc: mesh / MCU / SFU three communication architectures
短链接设计和思考
CMS垃圾收集器和G1垃圾收集器
数据库索引
数据中心碳中和之路,新华三如何全栈赋能?
【AI视野·今日Robot 机器人论文速览 第三十三期】Thu, 21 Apr 2022
防火门能申请BS 476-21 耐火测试吗?
Configure interceptor not to intercept swagger
14-Sprak设置自动分区
Postgre creates a sequence and binds it to a table field
关于log4j2的重新加载,以及不同级别日志输出到不同日志文件
WebRTC:Mesh/MCU/SFU三种通信架构
创建线程的四种方式
Can fire doors be tested according to en 1634-1?
mmocr DBLoss