Getting the first day in a week with T-SQL

本文介绍了一种在SQL Server中使用T-SQL获取当前周第一天日期的简洁方法。此外,还提供了一个从datetime类型中提取日期部分的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A colleague just asked me if I knew a way to get the first day in a week in SQL Server. While I'm sure that there are many ways to get the first day (date) in a week here is a very simple way that in my opinion is quite elegant. The example assumes that you want to get the date of the first day in the week that GETDATE() is in. You could of course replace GETDATE() with a column of the datetime type.

SELECT DATEADD(ww, DATEDIFF(ww,0,GETDATE()), 0)

The same method can be used to extract only the date part from a datetime in T-SQL:

DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

 

http://joelabrahamsson.com/getting-the-first-day-in-a-week-with-t-sql/

转载于:https://www.cnblogs.com/kkun/p/3367744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值