Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last day of last week in sql?

How to get last date of the lastweek in sql? I mean last sunday date using query?

like image 750
James123 Avatar asked May 07 '11 23:05

James123


People also ask

How do I get the last day of a date in SQL?

SQL Server EOMONTH() overview The EOMONTH() function returns the last day of the month of a specified date, with an optional offset. The EOMONTH() function accepts two arguments: start_date is a date expression that evaluates to a date. The EOMONTH() function returns the last day of the month for this date.

How do I get the first and last day of the week in SQL?

Option 1: Sunday as the First Day of the Week DATEADD(week, DATEDIFF(week, -1, RegistrationDate), -1) AS Sunday; The function DATEADD() takes three arguments: a datepart, a number, and a date.


1 Answers

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.

like image 157
Andriy M Avatar answered Oct 02 '22 19:10

Andriy M