SQL Server 中的第一个星期一 [英] First Monday of the Year in SQL Server

查看:32
本文介绍了SQL Server 中的第一个星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 T-SQL 找到年度的第一个星期一"?

How can I find "First Monday of the Year" using T-SQL ?

推荐答案

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

Here's the example from the link ngruson posted (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

这篇关于SQL Server 中的第一个星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆