SQL Server: Get week start and end dates of any month

by Rohit 19. January 2013 13:38

Get week start dates of any month

DECLARE @startDate DATETIME
SET @StartDate = '20130101'
;WITH DayCounter(n) AS
( SELECT 0 UNION ALL SELECT n+1 FROM DayCounter WHERE n<30)
SELECT DISTINCT DateAdd(dd,7-(DATEPART(DW,@StartDate+n)-1),@StartDate+n) FROM DayCounter
WHERE MONTH(DateAdd(dd,7-(DATEPART(DW,@StartDate+n)-1),@StartDate+n)) = MONTH(@StartDate)

 

Get week end dates of any month

DECLARE @StartDate DATETIME
SET @StartDate = '20130101'
;WITH DayCounter(n) AS
( SELECT 0 UNION ALL SELECT n+1 FROM DayCounter WHERE n<30)
SELECT DISTINCT DateAdd(dd,7-(DATEPART(DW,@StartDate+n)),@StartDate+n) FROM DayCounter
WHERE MONTH(DateAdd(dd,7-(DATEPART(DW,@StartDate+n)-1),@StartDate+n)) = MONTH(@StartDate)


Tags: , ,

SQL Server | Technology

Add comment

biuquote
  • Comment
  • Preview
Loading

About Rohit Prakash

Software Craftsman @ Fortune 100 Company. Technical Reviewer of few programming books.

A Single, Writer, Reader and Adventurist.

You can reach me at:

rohit [at] irohitable.com

Month List

Protected by Copyscape Web Plagiarism Software