;

How to Get First and Last Day of a Week in SQL Server


Tutorialsrack 30/04/2019 SQL SQL Server

In this article, we will learn how to get First day and Last Day of a current week, the previous week and the last week in SQL Server.

To get the first day of the previous week in SQL Server, a statement is as follow:

Example - To get the first day of the previous week in SQL Server
SELECT DATEADD(wk, -1, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) as 'First Day of the Previous Week';
Output

First Day of the Previous Week
---------------------------------------
2019-04-14 00:00:00.000

To get the last day of the previous week in SQL Server, a statement is as follow:

Example - To get the last day of the previous week in SQL Server
SELECT DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) as 'Last Day of the Previous Week';
Output

Last Day of the Previous Week
--------------------------------------
2019-04-20 00:00:00.000

To get the first day of the current week in SQL Server, a statement is as follow:

Example - To get the first day of the current week in SQL Server
SELECT DATEADD(wk, 0, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) as 'First Day of the Current Week';
Output

First Day of the Current Week
--------------------------------------
2019-04-21 00:00:00.000

To get the last day of the current week in SQL Server, a statement is as follow:

Example - To get the last day of the current week in SQL Server
SELECT DATEADD(wk, 1, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) as 'Last Day of the Current Week';
Output

Last Day of the Current Week
----------------------------
2019-04-27 00:00:00.000

To get the first day of the next week in SQL Server, a statement is as follow:

Example - To get the first day of the next week in SQL Server
SELECT DATEADD(wk, 1, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) as 'First Day of the Next Week';
Output

First Day of the Next Week
----------------------------------
2019-04-28 00:00:00.000

To get the last day of the next week in SQL Server, a statement is as follow:

Example - To get the last day of the next week in SQL Server
SELECT DATEADD(wk, 2, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) as 'Last Day Next Week';
Output

Last Day of the Next Week
---------------------------------
2019-05-04 00:00:00.000

I hope this article will help you to understand how to get First day and Last Day of a current week, the previous week and the last week in SQL Server.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags