首页 > 解决方案 > 我需要一个 SQL 服务器 Func 来计算一年中一周的完整日期

问题描述

我需要计算到一串 dd-MM-yyyy

来自 wwyy 字符串(我需要一周的第一天或一周中的任何其他天)

例如:现在我们在 18 周的第 42 周,所以如果我输入 4218,我将得到 15-10-2018 或 14-10-2018

BR,伊丹

标签: sql-server

解决方案


declare @input char(4) set @input = 4218

SET DATEFIRST 1
declare @wk int  set @wk = cast(SUBSTRING(@input,1,2) as int)
declare @yr int  set @yr = 2000 + cast(SUBSTRING(@input,3,2) as int)

select dateadd (week, @wk, dateadd (year, @yr-1900, 0)) - 4 -
         datepart(dw, dateadd (week, @wk, dateadd (year, @yr-1900, 0)) - 4) + 1

推荐阅读