简体   繁体   English

前一天的一周开始(星期一)

[英]week beginning (Monday) for previous day

I want to pull all the data for the week to the previous day.我想将本周的所有数据拉到前一天。 for example:例如:

if the date was Monday 2016-05-09.如果日期是 2016-05-09 星期一。 I would like the previous Monday 2016-05-02 to Sunday 2016-05-08.我想从前一个星期一 2016-05-02 到星期日 2016-05-08。

If the date was Tuesday 2016-05-10.如果日期是 2016-05-10 星期二。 I would like the previous Monday 2016-05-09 to Monday 2016-05-09.我想从前一个星期一 2016-05-09 到星期一 2016-05-09。

I have tried using with no joy.我试过不高兴地使用。

and (ShiftDate Between DATEADD(wk, 0, DATEADD(DAY, (1-DATEPART(WEEKDAY, GETDATE()-1)), DATEDIFF(dd, 0, GETDATE())))and GETDATE()-1)

You should use this:你应该使用这个:

and (ShiftDate Between (GETDATE() - 1 - (DATEPART(weekday, GETDATE() -1) + 7 - 2) % 7)
                       and (GETDATE()-1)
    )

Because Sunday is 1 in weekday so you should add 7 then module by 7 to get the different from nearest Monday (2 in weekday ) to the previous day.因为Sundayweekday中的 1,所以您应该添加7然后模块乘以7以获得从最近的Mondayweekday为 2)到前一天的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM