Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Starting and ending date of week? In Sql server?

In sql, how to get the starting and ending date of week for the particular month. Can you help me.

like image 787
Surya sasidhar Avatar asked Apr 04 '12 08:04

Surya sasidhar


People also ask

How do I get the start date and end date in SQL?

To use the dateadd function in SQL, we need to use the SELECT query followed by the DATEDIFF function and then return the output. The datePart should be provided only valid input (Check valid input table for datePart) and the startDate and the endDate should be provided with a date input in a proper format.

How do I get the start of the week from a date in SQL Server?

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

How do I get weekly week data in SQL?

WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.


3 Answers

The following will work whatever you consider the first day of the week (sunday, monday etc), just ensure you use SET DATEFIRST if you want to change from the default. SET DATEFIRST 1 will make the first day of the week monday.

SELECT  DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekStart],
        DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekEnd]

EDIT

I have just re-read you request and I think you may be after something different to what I have given above. If you want the day of the week of the first and last of the month this will do the trick:

SELECT  DATENAME(WEEKDAY, DATEADD(DAY, 1 - DATEPART(DAY, GETDATE()), GETDATE())) [FirstDayOfMonth],
        DATENAME(WEEKDAY, DATEADD(DAY, - DATEPART(DAY, DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) [LastDayOfMonth]
like image 108
GarethD Avatar answered Oct 02 '22 16:10

GarethD


To get first day of week use:

select dateadd(wk, datediff(wk, 0, getdate()), 0)

Last day will be first day + 6:

select dateadd(wk, datediff(wk, 0, getdate()), 0) + 6

Warning: this is culture sensitive. Check documentation on @@datefirst

like image 43
Nikola Markovinović Avatar answered Oct 02 '22 15:10

Nikola Markovinović


Your requirement is unclear: what do you consider to be the first and last days of a week? Sunday and Saturday? Monday and Friday? Monday and Sunday? But the best solution for many date-related queries is to create a calendar table. That gives you the ability to easily set the first and last days of weeks, months and years according to your definition and even in multiple formats if necessary.

This is especially useful if you work with concepts like financial years, business days, and so on where the definitions vary by country and even company. It is also the easiest solution when there are exceptions to your logic, e.g. the first week of the year has different rules than a 'normal' week.

With a calendar table you could pre-populate the first and last days of the week and then your query could be something like this:

-- first/last days stored as dates in their own columns
select FirstDayOfThisWeek, LastDayOfThisWeek
from dbo.Calendar
where BaseDate = @SomeDate

-- first/last days stored as bit flags
select max(BaseDate)
from dbo.Calendar
where BaseDate <= @SomeDate and IsFirstDayOfWeek = 0x1
like image 22
Pondlife Avatar answered Oct 02 '22 14:10

Pondlife