任何转换此varchar的想法:
201751
SQL中日期值的值:
2017年12月18日
答案 0 :(得分:1)
制作了几个变量来解析年份和周。然后使用DATEDIFF
计算周数,然后使用DATEADD
将周数添加到该值并显示日期。
DECLARE @InputValue as varchar(6) = '201751';
DECLARE @YearNum as varchar(4) = LEFT(@InputValue,4);
DECLARE @WeekNum as varchar(2) = RIGHT(@InputValue,2);
SELECT DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 6) AS StartOfWeek,
DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @YearNum) + (@WeekNum-1), 5) AS EndOfWeek;
结果:
StartOfWeek EndOfWeek
2017-12-17 00:00:00.000 2017-12-23 00:00:00.000
请注意,这仅适用于您的6个字符串输入,其中前4个字符是年份,最后 2个字符是月份。