我试过但无法得到正确的解决方案。我想要一个列出当年所有周末日期的 SQL 查询。
我试过这个 SQL 查询:
WITH hier(num, lvl) AS (
SELECT 0, 1
UNION ALL
SELECT 100, 1
UNION ALL
SELECT num + 1, lvl + 1
FROM hier
WHERE lvl < 100
)
SELECT lvl [Week],
convert(date,DATEADD(dw, -DATEPART(dw, DATEADD(wk,DATEDIFF(wk,0,'12/31/'+convert(nvarchar,YEAR(getdate()))), 0)+6 ),
DATEADD(wk, DATEDIFF(wk,0,'12/31/'+convert(nvarchar,YEAR(getdate()))), 0)+6 ) - num * 7,101) [End Date]
FROM hier a
where num < 52
ORDER BY [End Date] asc
它的输出是这样的:
Week End date
52 2012-01-14
51 2012-01-21
50 2012-01-28
49 2012-02-04
我希望日期从头开始 - 所以,上面缺少一个周末,即2012-07-01
. 另外,我希望周数显示为1, 2, 3...
而不是52, 51...
.