简体   繁体   中英

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. I would like the previous Monday 2016-05-02 to Sunday 2016-05-08.

If the date was Tuesday 2016-05-10. I would like the previous Monday 2016-05-09 to Monday 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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