我在这个网站上得到了一年中所有星期的代码,如下所示,我应该填充星期日期,开始日期为星期六,结束日期为星期五。当一周结束时,它应该进入下一周的日期。我怎么能做到这一点,请帮助我。
DECLARE @Year INT=2013;
DECLARE @start DATE;
--DECLARE @WK INT=2
SET @start = DATEADD(YEAR, @Year-1900, 0);
;WITH n AS
(
SELECT TOP (366) -- in case of leap year
TDate = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, @start)
FROM sys.all_objects
),
x AS
(
SELECT md = MIN(TDate) FROM n
WHERE DATEPART(WEEKDAY, TDate) = 7 -- assuming DATEFIRST is SATURDAY
),
y(TDate,wk) AS
(
SELECT n.TDate, ((DATEPART(DAYOFYEAR,n.TDate)-
DATEDIFF(DAY, @start,x.md)-1)/7)+1
FROM n CROSS JOIN x
WHERE n.TDate >= x.md
AND n.TDate < DATEADD(YEAR, 1, @start)
)
SELECT [date] = TDate, [week] = wk
FROM y WHERE wk < 53
ORDER BY [date];