Warm tip: This article is reproduced from serverfault.com, please click

其他-SQL Server年度的第一个星期一

(其他 - First Monday of the Year in SQL Server)

发布于 2012-11-23 08:58:07

如何使用T-SQL查找“年度第一个星期一”?

Questioner
HOY
Viewed
11
Cᴏʀʏ 2012-11-23 17:07:26

这是ngruson发布的链接(http://sqlbump.blogspot.nl/2010/01/first-monday-of-year.html)中的示例

DECLARE @Date datetime
DECLARE @Year int = 2012

SET @Date = DATEADD(YEAR, @Year - 1900, 0)

SELECT DATEADD(DAY, (@@DATEFIRST - DATEPART(WEEKDAY, @Date) + 
    (8 - @@DATEFIRST) * 2) % 7, @Date)

以上返回:

2012-01-02 00:00:00.000