Skip to content
Advertisement

Week number and its Sunday

I have a simple question.

If I have a week number, how I assigned a Sunday to it? (In t-sql)

For example. I have number 15 as input (which is week from 8.4. to 14.4., from monday to sunday), and I will need it to be shown as 14.4. as output. The things is, that I have a column of days converted to number of week (col1), and I need a Sunday of this week number (col2).

col1    col2
15      14.4.
15      14.4.
15      14.4.
15      14.4.
16      21.4.
16      21.4.
17      28.4.
17      28.4.
19      12.5.
19      12.5.

Advertisement

Answer

Use this, remember, you need know also the year, not only weekNo

Week in this case start on Monday and end on Sunday, you will get the sunday in requested week

 DECLARE @WeekNum int
 DECLARE @YearNum char(4)

 set @WeekNum = 46
 set @YearNum = 2019

  SELECT @WeekNum As WeekNo,  FORMAT(CONVERT(date,DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 6)), 'dd.MM.yyyy') AS SundayInWeek

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement