Previous Monday & previous Sunday's date based on today's date

后端 未结 6 1539
情话喂你
情话喂你 2020-12-05 00:46

I need the correct syntax to give me :

  1. Previous week\'s Monday\'s date based on the current date/time using GETDATE()
  2. Previous week\'s Su
6条回答
  •  半阙折子戏
    2020-12-05 00:58

    It should be noted that the issue with Sundays appears to no longer be present at least as of MSSQL 2012. Both the simple solution

    SELECT DATEADD(wk, DATEDIFF(wk, 6, @input), 0)
    

    and the complex one

    SELECT DATEADD(wk, DATEDIFF(wk, 6, 
    CASE DATEPART(dw,@input)
    WHEN 1 THEN DATEADD(d,-1,@input)
    ELSE @input
    END
    ), 0)
    

    return the same for any date that I've tried, including Sundays.

提交回复
热议问题