查找本周的开始日(星期一) [英] finding the start day (Monday) of the current week

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

问题描述

寻找可以确定本周开始日期(星期一)的 SQL 查询/查询.

Looking for a SQL query/queries that would determine the start day (Monday) of the current week.

示例:如果今天是 -> 那么一周的开始是

Example: If today is -> then the start of the week is

Sat Oct 09, 2010 -> Start of the week is Monday Oct 04, 2010
Sun Oct 10, 2010 -> Start of the week is Monday Oct 04, 2010
Mon Oct 11, 2010 -> Start of the week is Monday Oct 11, 2010
Tue Oct 12, 2010 -> Start of the week is Monday Oct 11, 2010

我在 Google 和 StackOverflow 上看到过很多解决方案".看起来像:

I have seen many "solutions" on Google and StackOverflow. The look something like:

SET @pInputDate = CONVERT(VARCHAR(10), @pInputDate, 111)
SELECT DATEADD(DD, 1 - DATEPART(DW, @pInputDate), @pInputDate)

这失败了,因为:2010 年 10 月 10 日星期日 -> 2010 年 10 月 11 日星期一(不正确).

This fails because: Sun Oct 10, 2010 -> start of week Monday Oct 11, 2010 (which is incorrect).

推荐答案

尝试使用 DATEFIRST 显式设置星期几被视为第一个".

Try using DATEFIRST to explicitly set the day of week to be regarded as the 'first'.

set DATEFIRST 1  --Monday
select DATEADD(DD, 1 - DATEPART(DW, @pInputDate), @pInputDate)

这将返回 InputDate 所在周的星期一.

This will return the Monday of the week the InputDate falls in.

这篇关于查找本周的开始日(星期一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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