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
得到两个时间之间的每一周
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
得到两个时间之间的每个月
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
得到两个时间之间的每个年份
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
原文:https://blog.csdn.net/weixin_33277597/article/details/82215827