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.

Finding Week Start and End Dates in SQL Server

Good luck to everyone in business and life.

302 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!