Finding Week Start and End Dates in SQL Server
Hello to everyone,
In this article, I will give you information about how to get the start and end date information of the week in SQL Server.
By default, SQL Server week start date is Sunday.
You can also do this using the code below.
DECLARE @dateTimeNow DATETIME = GETDATE();
SELECT [StartDate] = DATEADD(dd, - (DATEPART(WEEKDAY, @dateTimeNow) - 1), DATEADD(dd, DATEDIFF(dd, 0, @dateTimeNow), 0)),
[EndDate] = DATEADD(dd, 7 - (DATEPART(WEEKDAY, @dateTimeNow)), DATEADD(dd, DATEDIFF(dd, 0, @dateTimeNow), 0));
SELECT [StartDateTime] = DATEADD(
DAY,
- (DATEPART(WEEKDAY, @dateTimeNow) - 1),
DATEADD(DAY, DATEDIFF(DAY, 0, @dateTimeNow), 0)
),
[EndDateTime] = DATEADD(
DAY,
7 - (DATEPART(WEEKDAY, @dateTimeNow)),
DATEADD(SECOND, -1, DATEADD(DAY, DATEDIFF(DAY, 0, @dateTimeNow) + 1, 0))
);
When you run the code you will get the following result.
Good luck to everyone in business and life.