我正在创建一个存储过程来根据周数、月份数和年份显示日期范围内的某些记录。
例如:
Month = 4 [APRIL]
Week = 2 [Week 2 for APRIL]
Year = 2017
输出应为:[开始日期应为星期日]
STARTDATE | END DATE
4-2-2017 | 4-8-2017
到处搜索,这个已经很难了。
编辑: 我提供了一个示例查询供引用
@Month int,
@Year int,
@WeekNumber int
SELECT
(start date of the week based on week num, month, year) as DATE1,
(end date of the week based on week num, month, year) as DATE2
WHERE
StartDate = (start date of the week based on week num, month, year)
EndDate = (end date of the week based on week num, month, year)
最佳答案
也许这会有所帮助。
2008 年更新的示例
Declare @Month int = 4
Declare @Year int = 2017
Declare @Week int = 2
Select DateR1 = min(RetVal)
,DateR2 = max(RetVal)
From (
Select *,WkNr = Dense_Rank() over (Order by DatePart(WEEK,RetVal))
From (Select Top (40) RetVal=DateAdd(DD,-1+Row_Number() Over (Order By Number),cast(str((@Year*10000)+(@Month*100)+1,8) as date)) From master..spt_values) D
) A
Where WkNr=@Week
返回
DateR1 DateR2
2017-04-02 2017-04-08
关于sql - 在 SQL Server 中获取给定周数、月数和年份的一周的日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220543/