Query the last Sunday of the week of the given date (Sunday is the first day) SQL statement declare @ date & nbsp; datetimeset @ dategetdate () -- idea: the day of the week where the current log is located minus one week -- datepart (weekday, date) the returned value is related to @ datefirst setdatefirst7 -- or the SQL statement used to query the last Sunday (Sunday is the first day) of the week on which the given date is located.
Declare @ date datetime
Set @ date = getdate ()
-- Train of thought: minus one week on Sunday of the week where the current log is located
-- The return value of datepart (weekday, date) is related to @ datefirst.
Set datefirst 7 -- or set it to set language us_english; (Sunday is the First Day)
Select dateadd (week,-1, dateadd (day, 1-datepart (weekday, @ date), @ date) as 'Day of last week, Sunday'
-- One week equals 7 days
Select dateadd (day,-7, dateadd (day, 1-datepart (weekday, @ date), @ date) as 'Day of last week, Sunday'
-- Simplified
Select dateadd (day,-6-datepart (weekday, @ date), @ date) as 'Day of last week, Sunday'
-- Last Sunday, it has nothing to do with the SQL server language version or @ datefirst
Select dateadd (week,-1 + datediff (week,-1, @ date),-1) as 'last Sunday'
-- Or
Select dateadd (week, datediff (week, 6, @ date),-1) as 'last Sunday'
Go