ISO8601将周日期转换为日历日期

时间:2012-07-06 20:38:47

标签: sql sql-server datetime date

我正在尝试确定ISO 8601格式中给定的一周日期(即YYYY-W ##)的日期。我的最终目标是将ISO 8601周日期转换为ISO 8601日历日期。我需要在TSQL中执行此操作(我正在使用SQL Server 2005),我不确定SQL Server 05中是否有任何内置允许这样做,但它有助于查看另一种语言的示例或在通用伪代码中

更新:

很抱歉,如果我的问题结构令人困惑。基本上,我有一个ISO8601周日期,我正在尝试转换为ISO8601日历日期。

示例(来自Wikipedia

ISO 8601周日期:2012-W02(YYYY-W ##)

转换为...

ISO 8601日历日期:2012-01-09(YYYY-MM-DD)

由于我的周日期示例中未给出日期组件,因此可以假设一周的第一天。

2 个答案:

答案 0 :(得分:2)

尝试以下解决方案:

declare 
    @date varchar(10)
    ,@convertedDate datetime
    ,@wk int
    ,@yr int

set @date = '2012-W02';

set @yr = parsename(replace(@date, '-W', '.'), 2)
set @wk = parsename(replace(@date, '-W', '.'), 1)

set @convertedDate = convert(varchar(10), dateadd(week, @wk, dateadd (year, @yr-1900, 0)) - 5 - datepart(dw, dateadd (week, @wk, dateadd (year, @yr-1900, 0))), 121)

select 
    'Year' = @yr
    ,'Week' = @wk
    ,'Date' = @convertedDate

<强>输出:

-----------------------------------------
| Year | Week |         Date            |
-----------------------------------------
| 2012 |   2  | 2012-01-09 00:00:00.000 |
-----------------------------------------

答案 1 :(得分:0)

CREATE FUNCTION documentation给出适当的ISO周功能(对所有不良习惯进行一些调整,恕我直言):

CREATE FUNCTION dbo.ISOWeek 
( 
    @dt SMALLDATETIME 
) 
RETURNS TINYINT 
AS 
BEGIN 
    DECLARE @ISOweek TINYINT;

    SET @ISOweek = DATEPART(WEEK, @dt) + 1 
        -DATEPART(WEEK, RTRIM(YEAR(@dt)) + '0104');

    IF @ISOweek = 0 
    BEGIN 
        SET @ISOweek = dbo.ISOweek 
        ( 
            RTRIM(YEAR(@dt)-1)+'12'+RTRIM(24 + DAY(@dt)) 
        ) + 1;
    END 

    IF MONTH(@dt) = 12 AND DAY(@dt) - DATEPART(DAYOFWEEK, @dt) >= 28 
    BEGIN 
        SET @ISOweek = 1; 
    END 

    RETURN(@ISOweek);
END 
GO

我们可以创建一个这样的表:

CREATE TABLE dbo.ISOWeekCalendar
(
    [Date] SMALLDATETIME PRIMARY KEY,
    ISOWeekNumber TINYINT,
    [Year] INT,
    ISOWeek CHAR(8)
);
CREATE UNIQUE INDEX iw ON dbo.ISOWeekCalendar(ISOWeek);

我们可以使用任何年份的数据来填充它,这使用ISO周1-52代表2000 - 2029:

DECLARE @StartDate SMALLDATETIME,
        @EndDate   SMALLDATETIME;

SELECT @StartDate = '20000102',
       @EndDate   = '20291229';

INSERT dbo.ISOWeekCalendar([Date])
SELECT TOP (DATEDIFF(DAY, @StartDate, @EndDate)+1) n
 = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY s1.[object_id])-1, @StartDate)
 FROM sys.all_objects AS s1
 CROSS JOIN sys.all_objects AS s2
 ORDER BY s1.[object_id];

现在我们可以更新数据。

-- delete all non-Mondays:
SET DATEFIRST 1;
DELETE dbo.ISOWeekCalendar WHERE DATEPART(WEEKDAY, [Date]) <> 1;

-- put the proper ISO week number:
UPDATE dbo.ISOWeekCalendar SET ISOWeekNumber = dbo.ISOWeek([Date]);

-- put the year:
UPDATE dbo.ISOWeekCalendar SET [Year] = DATEPART(YEAR, [Date]);

-- update to the correct year for fringe days:
UPDATE dbo.ISOWeekCalendar SET [Year] = [Year] + 1 
  WHERE ISOWeekNumber = 1 AND MONTH([Date]) = 12;

-- finally, build the calculated value for YYYY-W##:
UPDATE dbo.ISOWeekCalendar 
  SET ISOWeek = RTRIM([Year]) + '-W' + RIGHT('0' + RTRIM(ISOWeekNumber), 2);

请注意,上述内容只需进行一次。现在我们可以根据输入运行一个非常简单的查询:

SELECT [Date] FROM dbo.ISOWeekCalendar WHERE ISOWeek = '2012-W02';

结果:

Date
-------------------
2012-01-09 00:00:00

我们甚至可以创建一个执行此操作的函数:

CREATE FUNCTION dbo.ISOWeekDate(@ISOWeek CHAR(8))
RETURNS SMALLDATETIME
WITH SCHEMABINDING
AS
BEGIN
  RETURN (SELECT [Date] FROM dbo.ISOWeekCalendar
    WHERE ISOWeek = @ISOWeek);
END
GO

另一种功能:

CREATE FUNCTION dbo.ISOWeekFromDate(@Date SMALLDATETIME)
RETURNS CHAR(8)
WITH SCHEMABINDING
AS
BEGIN
  RETURN (SELECT TOP (1) ISOWeek FROM dbo.ISOWeekCalendar
    WHERE [Date] <= @Date
    ORDER BY [Date] DESC);
END
GO

查询:

SELECT dbo.ISOWeekDate('2012-W02'), dbo.ISOWeekFromDate('20120110');

结果:

-------------------    --------
2012-01-09 00:00:00    2012-W02

是的,它比复杂的查询更加前期工作,但我更喜欢易用性和更清晰的查询语义。