Introduction


Here, We learn how to use some date time function to find out the week's start and end date simultaneously.
 
List of date time functions

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

Description


This article helps in displaying the start of the week date span and end of the week date span, which will be displayed, using SQL query.

Prerequisite

  • Sql Server

Date time functions


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 and end date using Sql Query


Week Start Date using Sql Query

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



Divide above Sql Query by passing parameter value

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



Week End Date using Sql Query

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



Divide above Sql Query by passing parameter value

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



Complete 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

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

See Also