How to get week numbers for whole year in SQL Server
My scenario: I have
SalesDate (eg: 2007-01-01 to 2016-01-01) filed like this up to
todate and have daily date. Now I have to find this
salesdate belong to which week.
Start day of week is
Sunday. Based on year, if new year starts week count start as 1.
One Solution collect form web for “How to get week numbers for whole year in SQL Server”
Try something like below,
DECLARE @SalesDate DATETIME = '2007-01-01' SELECT DATEDIFF(WEEK, DATEADD(WEEK, DATEDIFF(WEEK, 0, DATEADD(MONTH, DATEDIFF(MONTH, 0, @SalesDate), 0)), 0), @SalesDate - 1) + 1 WeekD, @SalesDate SalesDate
DECLARE @SalesDate DATETIME = '2007-01-01' SELECT DATEPART(WEEK,@SalesDate) WeekNumber, @SalesDate SalesDate