SQL Server 未显示给定日期的正确周数

发布于 2024-12-01 12:58:30 字数 908 浏览 1 评论 0原文

SQL Server 显示 2011 年第一周(1 月 1 日除外)的第 53 周,并且需要是第 1 周

。以下是查询和输出:

declare @T table (dt datetime)
insert into @T values
('2010-12-26'),
('2010-12-27'),
('2010-12-28'),
('2010-12-29'),
('2010-12-30'),
('2010-12-31'),
('2011-01-01'),
('2011-01-02'),
('2011-01-03'),
('2011-01-04'),
('2011-01-05'),
('2011-01-06'),
('2011-01-07'),
('2011-01-08')
select dt,DATEPART(wk,dt) from @T

输出:

2010-12-26 00:00:00.000 53
2010-12-27 00:00:00.000 53
2010-12-28 00:00:00.000 53
2010-12-29 00:00:00.000 53
2010-12-30 00:00:00.000 53
2010-12-31 00:00:00.000 53
2011-01-01 00:00:00.000 1
2011-01-02 00:00:00.000 2
2011-01-03 00:00:00.000 2
2011-01-04 00:00:00.000 2
2011-01-05 00:00:00.000 2
2011-01-06 00:00:00.000 2
2011-01-07 00:00:00.000 2
2011-01-08 00:00:00.000 2

我希望 SQL Server 显示从 12 月 26 日到 1 月 1 日的第 1 周。有人知道如何做到这一点吗?

谢谢和问候, 阿施温。

SQL Server is showing week 53 for first week of 2011 except 1th of January, and needs to be week 1.

Below is the query and output:

declare @T table (dt datetime)
insert into @T values
('2010-12-26'),
('2010-12-27'),
('2010-12-28'),
('2010-12-29'),
('2010-12-30'),
('2010-12-31'),
('2011-01-01'),
('2011-01-02'),
('2011-01-03'),
('2011-01-04'),
('2011-01-05'),
('2011-01-06'),
('2011-01-07'),
('2011-01-08')
select dt,DATEPART(wk,dt) from @T

Output:

2010-12-26 00:00:00.000 53
2010-12-27 00:00:00.000 53
2010-12-28 00:00:00.000 53
2010-12-29 00:00:00.000 53
2010-12-30 00:00:00.000 53
2010-12-31 00:00:00.000 53
2011-01-01 00:00:00.000 1
2011-01-02 00:00:00.000 2
2011-01-03 00:00:00.000 2
2011-01-04 00:00:00.000 2
2011-01-05 00:00:00.000 2
2011-01-06 00:00:00.000 2
2011-01-07 00:00:00.000 2
2011-01-08 00:00:00.000 2

I want SQL Server to show week 1 from Dec 26th - Jan 1th. Does anybody know how to accomplish this?

Thanks and regards,
Aschwin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佞臣 2024-12-08 12:58:30

这比我最初预期的要困难得多。我正在对比去年年底,看看是否有资格进入新的一年。如果是这样,我将周设置为 1,否则我只使用正常周。

declare @T table (dt datetime) 
insert into @T values 
('2010-12-25'), 
('2010-12-26'), 
('2010-12-27'), 
('2010-12-28'), 
('2010-12-29'), 
('2010-12-30'), 
('2010-12-31'), 
('2011-01-01'), 
('2011-01-02'), 
('2011-01-03'), 
('2011-01-04'), 
('2011-01-05'), 
('2011-01-06'), 
('2011-01-07'), 
('2011-01-08'), 
('2011-12-31'),
('2012-01-01')

select dt, 
week = case when dt + 6 - datediff(day, -1, dt) % 7 = dateadd(year, datediff(year,-1, dt), 0)
then 1 else datepart(week, dt) end from @t

证明:

https://data.stackexchange.com/stackoverflow/q/110527/

It was alot harder than I first expected. I am comparing the end of last year to see if it is qualified to be part of the new year. If so i set the week as 1, otherwise i just use the normal week.

declare @T table (dt datetime) 
insert into @T values 
('2010-12-25'), 
('2010-12-26'), 
('2010-12-27'), 
('2010-12-28'), 
('2010-12-29'), 
('2010-12-30'), 
('2010-12-31'), 
('2011-01-01'), 
('2011-01-02'), 
('2011-01-03'), 
('2011-01-04'), 
('2011-01-05'), 
('2011-01-06'), 
('2011-01-07'), 
('2011-01-08'), 
('2011-12-31'),
('2012-01-01')

select dt, 
week = case when dt + 6 - datediff(day, -1, dt) % 7 = dateadd(year, datediff(year,-1, dt), 0)
then 1 else datepart(week, dt) end from @t

Proof:

https://data.stackexchange.com/stackoverflow/q/110527/

一紙繁鸢 2024-12-08 12:58:30

我不确定它是否适用于所有年份(但看起来像),但您可以使用 CASE 语句。

SELECT  dt
        , CASE  WHEN DATEPART(wk, dt) <> 53 
                THEN DATEPART(wk, dt) 
                ELSE 1 
          END  
FROM    @T

新的 ISO_WEEK 日期部分不适用于您请求的输出。

I am not sure it holds for all years (but it looks like it) but you could solve this using a CASE statement.

SELECT  dt
        , CASE  WHEN DATEPART(wk, dt) <> 53 
                THEN DATEPART(wk, dt) 
                ELSE 1 
          END  
FROM    @T

The new ISO_WEEK datepart doesn't apply to your requested output.

聽兲甴掵 2024-12-08 12:58:30

我创建了 2 个函数来处理这个问题
1)获取一周的第一天或最后一天
2)获取周数或年份

函数1

    CREATE FUNCTION [dbo].[fn_GetDayOf]
(
    @Date datetime,
    --@FirstDayOfWeek int = 7,
    @Mode int =1
)
/*
    Mode 1: First Day Of Week
    Mode 2: Last Day Of Week
*/

RETURNS datetime
WITH EXECUTE AS CALLER
BEGIN
    Declare @Return datetime

    --SET DATEFIRST @FirstDayOfWeek

    IF @Mode = 1
    BEGIN
        select @Return = dateadd(day,-(datepart(weekday,@date)-1),convert(date,@date))
    END
    ELSE IF @Mode = 2
    BEGIN
        select @Return = dateadd(SECOND,-1,convert(datetime,dateadd(day,(datepart(weekday,@date)),convert(date,@date))))
    END
    ELSE
    BEGIN
        SET @Return = @Date
    END
    --SET DATEFIRST 7

    RETURN @Return
END

函数2

    CREATE FUNCTION [dbo].[fn_GetYearWeek]
(
    @Date datetime,
    --@FirstDayOfWeek int = 7,
    @Mode int =1
)
/*
    Mode 1 = Week Number
    Mode 2 = Year
*/
RETURNS INT
BEGIN
    declare @Return int
    IF @Mode = 1
    BEGIN
        select @Return = case when  datepart(week,[dbo].[fn_GetDayOf] (@Date,1)) <> datepart(week,[dbo].[fn_GetDayOf] (@Date,2)) then datepart(week,[dbo].[fn_GetDayOf] (@Date,1)) else datepart(week,[dbo].[fn_GetDayOf] (@Date,2)) end 
    END
    ELSE IF @Mode = 2
    BEGIN
        select @Return = case when  datepart(WEEK,[dbo].[fn_GetDayOf] (@Date,1)) <> datepart(week,[dbo].[fn_GetDayOf] (@Date,2)) then datepart(YEAR,[dbo].[fn_GetDayOf] (@Date,1)) else datepart(YEAR,[dbo].[fn_GetDayOf] (@Date,2)) end 
    END
    ELSE
    BEGIN
        SET @Return = -1
    END

    Return  @Return
END

运行示例

declare @T table (dt datetime) 
insert into @T values 
('2010-12-25'), 
('2010-12-26'), 
('2010-12-27'), 
('2010-12-28'), 
('2010-12-29'), 
('2010-12-30'), 
('2010-12-31'), 
('2011-01-01'), 
('2011-01-02'), 
('2011-01-03'), 
('2011-01-04'), 
('2011-01-05'), 
('2011-01-06'), 
('2011-01-07'), 
('2011-01-08'), 
('2011-12-31'),
('2012-01-01'),
('2012-01-02'),
('2012-12-31'),
('2013-01-01')

select 
    dt,
    datepart(week,dt),
    --case when  datepart(week,[dbo].[fn_GetDayOf] (dt,1)) <> datepart(week,[dbo].[fn_GetDayOf] (dt,2)) then datepart(week,[dbo].[fn_GetDayOf] (dt,1)) else datepart(week,[dbo].[fn_GetDayOf] (dt,2)) end 
    [dbo].[fn_GetYearWeek] (dt,1),
    [dbo].[fn_GetYearWeek] (dt,2)
from @T

结果:

I Created 2 functions to deal with this issue
1) to get First or last day of the week
2) to get the week number or year

function 1

    CREATE FUNCTION [dbo].[fn_GetDayOf]
(
    @Date datetime,
    --@FirstDayOfWeek int = 7,
    @Mode int =1
)
/*
    Mode 1: First Day Of Week
    Mode 2: Last Day Of Week
*/

RETURNS datetime
WITH EXECUTE AS CALLER
BEGIN
    Declare @Return datetime

    --SET DATEFIRST @FirstDayOfWeek

    IF @Mode = 1
    BEGIN
        select @Return = dateadd(day,-(datepart(weekday,@date)-1),convert(date,@date))
    END
    ELSE IF @Mode = 2
    BEGIN
        select @Return = dateadd(SECOND,-1,convert(datetime,dateadd(day,(datepart(weekday,@date)),convert(date,@date))))
    END
    ELSE
    BEGIN
        SET @Return = @Date
    END
    --SET DATEFIRST 7

    RETURN @Return
END

Function 2

    CREATE FUNCTION [dbo].[fn_GetYearWeek]
(
    @Date datetime,
    --@FirstDayOfWeek int = 7,
    @Mode int =1
)
/*
    Mode 1 = Week Number
    Mode 2 = Year
*/
RETURNS INT
BEGIN
    declare @Return int
    IF @Mode = 1
    BEGIN
        select @Return = case when  datepart(week,[dbo].[fn_GetDayOf] (@Date,1)) <> datepart(week,[dbo].[fn_GetDayOf] (@Date,2)) then datepart(week,[dbo].[fn_GetDayOf] (@Date,1)) else datepart(week,[dbo].[fn_GetDayOf] (@Date,2)) end 
    END
    ELSE IF @Mode = 2
    BEGIN
        select @Return = case when  datepart(WEEK,[dbo].[fn_GetDayOf] (@Date,1)) <> datepart(week,[dbo].[fn_GetDayOf] (@Date,2)) then datepart(YEAR,[dbo].[fn_GetDayOf] (@Date,1)) else datepart(YEAR,[dbo].[fn_GetDayOf] (@Date,2)) end 
    END
    ELSE
    BEGIN
        SET @Return = -1
    END

    Return  @Return
END

Running Example

declare @T table (dt datetime) 
insert into @T values 
('2010-12-25'), 
('2010-12-26'), 
('2010-12-27'), 
('2010-12-28'), 
('2010-12-29'), 
('2010-12-30'), 
('2010-12-31'), 
('2011-01-01'), 
('2011-01-02'), 
('2011-01-03'), 
('2011-01-04'), 
('2011-01-05'), 
('2011-01-06'), 
('2011-01-07'), 
('2011-01-08'), 
('2011-12-31'),
('2012-01-01'),
('2012-01-02'),
('2012-12-31'),
('2013-01-01')

select 
    dt,
    datepart(week,dt),
    --case when  datepart(week,[dbo].[fn_GetDayOf] (dt,1)) <> datepart(week,[dbo].[fn_GetDayOf] (dt,2)) then datepart(week,[dbo].[fn_GetDayOf] (dt,1)) else datepart(week,[dbo].[fn_GetDayOf] (dt,2)) end 
    [dbo].[fn_GetYearWeek] (dt,1),
    [dbo].[fn_GetYearWeek] (dt,2)
from @T

result:

寄与心 2024-12-08 12:58:30

另一种检索当年总周数的方法:

DECLARE @LASTDAY DATETIME
DECLARE @weeks INT
SET @LASTDAY = DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)))
SELECT @weeks = CASE DATEname(dw,@LASTDAY)

    WHEN 'MONDAY' THEN DATEPART(WK, DATEADD(wk,DATEDIFF(wk,7,@LASTDAY),5))
    WHEN 'TUESDAY' THEN DATEPART(WK, DATEADD(wk,DATEDIFF(wk,7,@LASTDAY),5))
    WHEN 'WEDNESDAY' THEN DATEPART(WK, DATEADD(wk,DATEDIFF(wk,7,@LASTDAY),5))
    ELSE DATEPART(WK, @LASTDAY)
END
select @weeks

Another way to retrieve the total number of weeks in current year:

DECLARE @LASTDAY DATETIME
DECLARE @weeks INT
SET @LASTDAY = DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)))
SELECT @weeks = CASE DATEname(dw,@LASTDAY)

    WHEN 'MONDAY' THEN DATEPART(WK, DATEADD(wk,DATEDIFF(wk,7,@LASTDAY),5))
    WHEN 'TUESDAY' THEN DATEPART(WK, DATEADD(wk,DATEDIFF(wk,7,@LASTDAY),5))
    WHEN 'WEDNESDAY' THEN DATEPART(WK, DATEADD(wk,DATEDIFF(wk,7,@LASTDAY),5))
    ELSE DATEPART(WK, @LASTDAY)
END
select @weeks
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文