我已經在一年中的所有星期在這個網站的代碼如下,我應該填充星期日期,開始日期爲星期六和結束日期爲星期五。當一週結束時,它應該進入下一週的日期。 我怎麼能達到這個請幫助我。顯示整個星期的開始日期爲星期六
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];
我不明白你的問題,你期望輸出什麼?你有沒有考慮過使用[日曆表](http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html)而不是複雜的查詢?或者這是爲了填充日曆表? – Pondlife 2013-02-13 17:14:27