Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct date from year and week number in MSSQL

Tags:

sql-server

If I have a YEAR and a WEEK number, what's a clean way to construct a DATE from this? I'd prefer it if the day of the week could be a Monday.

like image 784
BigSmoke Avatar asked Sep 09 '15 08:09

BigSmoke


1 Answers

Use DATEADD

Rextester Demo

DECLARE @y INT = 2015, 
        @w INT = 37;

SELECT 
  [StartOfWeek] = DATEADD(wk,DATEDIFF(wk,7,CAST(@y AS NVARCHAR(100))) + (@w-1),7);
like image 51
Lukasz Szozda Avatar answered Oct 20 '22 16:10

Lukasz Szozda