[英]Get Date from ISO Week in SQL
I created a function in SQL to return the proper date from a ISO Week from a format like "15W53". 我在SQL中创建了一个函数,以从“ ISO周”以“ 15W53”之类的格式返回正确的日期。 Where the first part before the "W" is the year number and the second half is the week number. “ W”之前的第一部分是年份数字,后半部分是星期数字。 The returned date should return the start of the week for that date. 返回的日期应返回该日期的一周的开始。
So for example, 15W53 should return 12-28-2015 and 16W01 should return 01-04-2016. 因此,例如15W53应该返回2015年12月28日,而16W01应该返回2016年1月4日。 However if I run this for the following examples I get, from what I have read about ISO week, incorrect results. 但是,如果我在下面的示例中运行该程序,从我所了解的有关ISO周的信息中得出的结果是错误的。
Did I create the function incorrectly to parse the date? 我是否错误地创建了函数以解析日期?
SET DATEFIRST 1;
SELECT dbo.[GetDateFromISOweek]('15W52') AS Correct, '15W52' -- Returns: 2015-12-21
SELECT dbo.[GetDateFromISOweek]('15W53') AS Correct, '15W53' -- Returns: 2015-12-28
SELECT dbo.[GetDateFromISOweek]('16W01') AS Incorrect, '16W01'-- Returns: 2015-12-28
SELECT dbo.[GetDateFromISOweek]('16W02') AS Incorrect, '16W02'-- Returns: 2016-01-04
SELECT dbo.[GetDateFromISOweek]('16W03') AS Incorrect, '16W03'-- Returns: 2016-01-11
Function: 功能:
CREATE FUNCTION [dbo].[GetDateFromISOweek] (@Input VARCHAR(10))
RETURNS DATETIME
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @YearNum CHAR(4)
DECLARE @WeekNum VARCHAR(2)
SET @YearNum = SUBSTRING(@Input,0,CHARINDEX('W',@Input,0))
SET @WeekNum = SUBSTRING(@Input,CHARINDEX('W',@Input,0)+1,LEN(@Input))
RETURN(DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-1), 7));
END;
Change the -1 in your return to calculate if the first week of the year should be consider the first one or not (if have more than 3 days). 更改您的纳税申报单中的-1以计算是否应该将一年的第一周考虑为第一周(如果超过三天)。 Something like this 像这样
case when DATEDIFF ( day , convert(datetime,'01/01/'+ @YearNum),@FirstDay )>=3 then 1 else 0 end
The full code, including the first sunday, can be improved, but works... 完整的代码,包括第一个星期天,可以进行改进,但是可以...
CREATE FUNCTION [dbo].[GetDateFromISOweek] (@Input VARCHAR(10))
RETURNS DATETIME
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @YearNum CHAR(4)
DECLARE @WeekNum VARCHAR(2)
declare @FirstDay datetime
SET @YearNum = cast(SUBSTRING(@Input,0,CHARINDEX('W',@Input,0)) as int)+2000
SET @WeekNum = SUBSTRING(@Input,CHARINDEX('W',@Input,0)+1,LEN(@Input))
set @FirstDay=DATEADD(DAY, (@@DATEFIRST - DATEPART(WEEKDAY, DATEADD(YEAR, @YearNum - 1900, 0)) + (8 - @@DATEFIRST) * 2) % 7, DATEADD(YEAR, @YearNum - 1900, 0))-1
RETURN(DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @YearNum) + (@WeekNum-case when DATEDIFF ( day , convert(datetime,'01/01/'+ @YearNum),@FirstDay )>=3 then 1 else 0 end), 7));
END;
go
SET DATEFIRST 1;
SELECT dbo.[GetDateFromISOweek]('15W52'),'15W52' union
SELECT dbo.[GetDateFromISOweek]('15W53'),'15W53' union
SELECT dbo.[GetDateFromISOweek]('16W01'), '16W01' union
SELECT dbo.[GetDateFromISOweek]('16W02'), '16W02' union
SELECT dbo.[GetDateFromISOweek]('16W03'), '16W03'
The result will be 结果将是
----------------------- -----
2015-12-21 00:00:00.000 15W52
2015-12-28 00:00:00.000 15W53
2016-01-04 00:00:00.000 16W01
2016-01-11 00:00:00.000 16W02
2016-01-18 00:00:00.000 16W03
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.