How to get last day of last week in sql?

Regardless of the actual DATEFIRST setting, the last Sunday could be found like this:

SELECT DATEADD(day,
               -1 - (DATEPART(weekday, GETDATE()) + @@DATEFIRST - 2) % 7,
               GETDATE()
              ) AS LastSunday

Replace GETDATE() with a parameter @date to get the last Sunday before a particular date.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)