周结束 - 需要周一

时间:2017-03-20 21:21:01

标签: date sql-server-2012

我有一个SQL查询,我需要将周结束日期显示为星期一。我测试了以下代码,但在星期六,星期日,星期一等都收到了回复。请帮帮忙?

SELECT DATEADD(DD, 7-(DATEPART(DW, MIN([Date])))
  , MIN([Date])) AS EndOfWeek, COUNT(*) AS TermsPerWeek

FROM Table

WHERE [Date] NOT LIKE 'null' 
 AND ([Date] LIKE '%2016%' OR [Date] LIKE '%2017%')

GROUP BY DATEPART(WEEK,[Date]);

1 个答案:

答案 0 :(得分:0)

使用set datefirst

set datefirst = 2

这会将本周的开始视为星期二,将结尾视为星期一。