Вопрос:

sql server friday date from week number

sql-server date

57 просмотра

1 ответ

381 Репутация автора

I have the following table in MS SQL Server that has year number and week number:

Year               Week
-----             ------
2015               33
2014               41
2016               40

How can I get a 3rd column that has the date of the corresponding Friday for each row? So, week 33 of 2015 should return: Friday, August 14, 2015. I have tried using Datefromparts(year, 1, week*7-1), because an equivalent function in Excel using Date seemed to do the trick in Excel, but Datefromparts doesn't work with integers outside valid ranges.

Thanks.

Автор: Thomas Moore Источник Размещён: 08.11.2017 10:40

Ответы (1)


2 плюса

6816 Репутация автора

Решение

Try this

DECLARE @Year INT = 2016
DECLARE @Week INT = 40

SELECT DATEADD(dd, 5, (DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + CAST(@Year AS VARCHAR)) + (@Week-1), 6)))
Автор: SQLChao Размещён: 08.11.2017 11:10
Вопросы из категории :
32x32