Get Week Start Date & Week End Date Using SQL Server

Introduction

Here, I used some date time function to find out the week's start and end date simultaneously.

List of date time functions

  • DATEADD()
  • DATEPART()
  • GETDATE()
  • CAST()  

DATEADD()

It returns a particular date with the particular number interval added to a particular date part of the date.

DATEPART()

DATEPART () function returns the integer value of particular datepart of the passed date.

This function returns the int value. Datepart(datepart, date) takes the datepart and date i.e. 2 parameters.

Datepart is a part of date, e.g. day, month, year. 

GETDATE()

Returns the current database system timestamp as a datetime value. This value is derived from the operating system of the computer on which the instance of SQL Server is running.

CAST()

Converts an expression of one data type to another.

Week_Start_Date select statement

SELECT DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_Start_Date] 

Divide Week_Start_Date select statement

select DATEPART(WEEKDAY, GETDATE())  
select CAST(GETDATE() AS DATE)  
SELECT  DATEADD(DAY, 2 - 5, '2017-04-06') [Week_Start_Date] 

Week_End_Date select statement

Select DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_End_Date] 

Divide Week_End_Date select statement

select DATEPART(WEEKDAY, GETDATE())  
select CAST(GETDATE() AS DATE)  
SELECT  DATEADD(DAY, 8 - 5, '2017-04-06') [Week_End_Date] 

Full query for week start date & week end date

SELECT  DATEADD(DAY, 2 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_Start_Date],  
DATEADD(DAY, 8 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [Week_End_Date] 

Summary

  1. Date time function in SQL Server.
  2. To use Date time function, we can find out week start date and week end date.