1

如果我给出的日期为 2015-12-31,它必须显示为

    2015W53, for 2015-12-31, which means Week 53, and next follows as
    2016W1,  Next as
    2016W2,  Next as
    2016W3   Next as...

我的逻辑是

    DECLARE @WEEK VARCHAR(2), @YEAR VARCHAR(4) 
    SELECT @YEAR = DATEPART(YY,@DATE), @WEEK = CAST(DATEPART(WK,@DATE) AS INT) % 52 ; 
     IF(CAST(DATEPART(WK,@DATE) AS INT) % 52 = 0) 
       SET @WEEK = 52; 
     IF(CAST(DATEPART(WK,@DATE) AS INT) = 53) 
       SET @YEAR = CAST(@YEAR AS INT) + 1; 
     IF (LEN(@WEEK) < 2) 
    BEGIN 
      SET @WEEK = LEFT('0' + @week, 2) 
    END 
    RETURN @YEAR + @WEEK

但是这里2015W53不见了,接下来 9 周,

我也只想要一年中的最后一周..,尝试了很多次但无法获得。任何帮助都非常感谢。

4

4 回答 4

1

您可以根据需要连接。也不需要循环

SELECT
    YEAR(DATEADD(week, X.Y, BaseDate)),
    DATEPART(ISO_WEEK, DATEADD(week, X.Y, BaseDate))
FROM
    (SELECT CAST('20151231' AS date) AS BaseDate) D
    CROSS JOIN
    (
    VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8)
    ) AS X(Y);

更新SQL Server 2008 添加了修复周编号的ISO_WEEK 日期部分

这几乎可行,因为根据 SQL Server,第 2 周从 2016 年 1 月 3 日开始。这与 ISO周编号不同。

于 2013-05-28T12:41:08.267 回答
0

您可能不会设置@WEEKDATEPART(WK,@DATE) % 52,因为有些年份超过 52 周!如果每年正好有 52 周,事情就简单了。

为什么不直接确定给定的星期@DATE,然后使用DATEADD添加一周,看看会发生什么?你这样做 9 次,你就会得到你想要的。

例如:

DECLARE @yearsAndWeeks TABLE (Year INT, Week INT)

DECLARE @i INT
SET @i = 0

DECLARE @Date DATETIME
SET @Date = '20151231'

WHILE @i < 9
BEGIN
    INSERT INTO @yearsAndWeeks (Year, Week) VALUES (DATEPART(yy, @Date), DATEPART(ISO_WEEK, @Date))
    SET @Date = DATEADD(WK, 1, @Date)
    SET @i = @i + 1
END

SELECT * FROM @yearsAndWeeks
于 2013-05-28T12:40:04.800 回答
0

@Date是您想要周数据的开始日期, @ NumberOfWeeks是您想要数据的后续周数。请根据需要修改这些值。

 --Create the temp table to hold the data
    IF OBJECT_ID('TempDB..#Dates') IS NOT NULL
    DROP TABLE #Dates;

CREATE TABLE #Dates
(   [Year]          INT NOT NULL,
    [WeekNumber]    INT NOT NULL,
    [RequiredWeek]  NVARCHAR(255) NOT NULL,
    [Date]          DATETIME NOT NULL
);


--DECLARE @Date        Datetime='2016-1-1';   --for testing
--DECLARE @Date        Datetime='2016-12-31'; --for testing
DECLARE @Date          Datetime='2015-12-31';
DECLARE @Days          INT;
DECLARE @NumberOfWeeks INT;
DECLARE @Ctr           INT=0;

SET @NumberOfWeeks=9 

WHILE @Ctr<@NumberOfWeeks
BEGIN
    SET @Days=@Ctr*7

    INSERT INTO #Dates
    SELECT  DATEPART(YEAR,@Date+@Days) AS [Year],
            DATEPART(wk,@Date+@Days) AS [WeekNumber],
            CAST(DATEPART(YEAR,@Date+@Days) AS VARCHAR(10))+'W'+ CAST(DATEPART(wk,@Date+@Days) AS VARCHAR(10)) AS [RequiredWeek],
            @Date+@Days AS [Date]
    SET @Ctr=@Ctr+1
END

SELECT * FROM #Dates;

输出:

Year    WeekNumber  RequiredWeek    Date
2015    53          2015W53          2015-12-31 00:00:00.000
2016    2           2016W2           2016-01-07 00:00:00.000
2016    3           2016W3           2016-01-14 00:00:00.000
2016    4           2016W4           2016-01-21 00:00:00.000
2016    5           2016W5           2016-01-28 00:00:00.000
2016    6           2016W6           2016-02-04 00:00:00.000
2016    7           2016W7           2016-02-11 00:00:00.000
2016    8           2016W8           2016-02-18 00:00:00.000
2016    9           2016W9           2016-02-25 00:00:00.000
于 2013-05-28T13:25:29.933 回答
0

为了进一步根据您的需求定制它,并冒着使此解决方案非通用的风险,这里是假设开始日期是“2015 年 12 月 31 日”,在 2015W53 之后给出 2016W1 的代码

--Create the temp table
IF OBJECT_ID('TempDB..#Dates') IS NOT NULL
DROP TABLE #Dates;

CREATE TABLE #Dates
(   [Year]          INT NOT NULL,
    [WeekNumber]    INT NOT NULL,
    [RequiredWeek]  NVARCHAR(255) NOT NULL,
    [Date]          DATETIME NOT NULL
);


--DECLARE @Date Datetime='2016-1-1';
--DECLARE @Date Datetime='2016-12-31';
DECLARE @Date Datetime='2015-12-31';
DECLARE @Days INT;
DECLARE @NumberOfWeeks INT;
DECLARE @Ctr INT=0;

SET @NumberOfWeeks=9 

WHILE @Ctr<@NumberOfWeeks
BEGIN
    IF (@Date ='2015-12-31' AND @Ctr=1)  -- To handle 1st week of Jan 2016
    SET @Days=@Ctr+1
    else if (@Date ='2015-12-31' AND @Ctr=0) --To Handle last week of Dec 2015
    SET @Days=(@Ctr)*7
    else
    SET @Days=(@Ctr-1)*7   --for rest of the weeks of 2016

    INSERT INTO #Dates
    SELECT  DATEPART(YEAR,@Date+@Days) AS [Year],
            DATEPART(wk,@Date+@Days) AS [WeekNumber],
            CAST(DATEPART(YEAR,@Date+@Days) AS VARCHAR(10))+'W'+ CAST(DATEPART(wk,@Date+@Days) AS VARCHAR(10)) AS [RequiredWeek],
            @Date+@Days AS [Date]
    SET @Ctr=@Ctr+1
END

SELECT * FROM #Dates;
于 2013-05-28T13:43:48.793 回答