用户登录
用户注册

分享至

SQL Server 中的第一个星期一

  • 作者: 安静祝福她
  • 来源: 51数据库
  • 2022-10-21

问题描述

如何使用 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
软件
前端设计
程序设计
Java相关