SQL: how to get the sunday of the current week

I want to get the last day (Sunday) of the current week with any timestamp. I tried following the script, but it returns Saturday as the last day, not Sunday, as I expected.

Select DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),GETDATE()) AS 'Last Day Of Week' 

Any answer is welcome!

+6
source share
2 answers

It will work if you change the standard DATEFIRST from Sunday (7) to Monday (1):

 SET DATEFIRST 1 Select DATEADD(DAY , 7-DATEPART(WEEKDAY,GETDATE()),GETDATE()) AS 'Last Day Of Week' 
+11
source

Prefix your command with set datefirst 1

0
source

Source: https://habr.com/ru/post/920968/


All Articles