在SQL Server中使用DATEPART的周数错误

时间:2021-12-13 01:45:36

I've got the problem that

我遇到了这个问题

select datepart(ww, '20100208')

is returning as result week 7 in SQL Server 2000. But 08.02.2010 should be week 6 according to the ISO 8601 specification! This is causing problems in delivery week calculations.

在第7周在SQL Server 2000中返回结果。但是根据ISO 8601规范,08.02.2010应该是第6周!这导致交货周计算出现问题。

What should I do to get week number values according to ISO 8601?

我该怎么做才能获得符合ISO 8601的周数值?

2 个解决方案

#1


12  

You can do this within SQL 2008 very easily as it now supports isoww as the first datepart argument. However, this wasn't in SQL 2000 (or 2005). There is a function in this article which will do it for you in SQL 2000/2005.

您可以非常轻松地在SQL 2008中执行此操作,因为它现在支持isoww作为第一个datepart参数。但是,这不是在SQL 2000(或2005)中。本文中有一个函数可以在SQL 2000/2005中为您完成。

In case the blog goes offline, here is the function. Go to the post to learn more about ISO and non-ISO weeks.

如果博客离线,这是功能。转到帖子了解有关ISO和非ISO周的更多信息。

CREATE FUNCTION ISOweek  (@DATE datetime)
RETURNS int
AS
BEGIN
   DECLARE @ISOweek int
   SET @ISOweek= DATEPART(wk,@DATE)+1
      -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104')
   --Special cases: Jan 1-3 may belong to the previous year
   IF (@ISOweek=0) 
      SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1 
         AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1
   --Special case: Dec 29-31 may belong to the next year
   IF ((DATEPART(mm,@DATE)=12) AND 
      ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
      SET @ISOweek=1
   RETURN(@ISOweek)
END

#2


0  

One easy way to do this is to use isowk instead of wk as shown here:

一个简单的方法是使用isowk而不是wk,如下所示:

select datepart(isowk, '2010-02-08');

Like @MicSim and @AdaTheDev mentioned, this will only work in newer versions (>=2008).

与@MicSim和@AdaTheDev一样,这只适用于较新的版本(> = 2008)。

#1


12  

You can do this within SQL 2008 very easily as it now supports isoww as the first datepart argument. However, this wasn't in SQL 2000 (or 2005). There is a function in this article which will do it for you in SQL 2000/2005.

您可以非常轻松地在SQL 2008中执行此操作,因为它现在支持isoww作为第一个datepart参数。但是,这不是在SQL 2000(或2005)中。本文中有一个函数可以在SQL 2000/2005中为您完成。

In case the blog goes offline, here is the function. Go to the post to learn more about ISO and non-ISO weeks.

如果博客离线,这是功能。转到帖子了解有关ISO和非ISO周的更多信息。

CREATE FUNCTION ISOweek  (@DATE datetime)
RETURNS int
AS
BEGIN
   DECLARE @ISOweek int
   SET @ISOweek= DATEPART(wk,@DATE)+1
      -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104')
   --Special cases: Jan 1-3 may belong to the previous year
   IF (@ISOweek=0) 
      SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1 
         AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1
   --Special case: Dec 29-31 may belong to the next year
   IF ((DATEPART(mm,@DATE)=12) AND 
      ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
      SET @ISOweek=1
   RETURN(@ISOweek)
END

#2


0  

One easy way to do this is to use isowk instead of wk as shown here:

一个简单的方法是使用isowk而不是wk,如下所示:

select datepart(isowk, '2010-02-08');

Like @MicSim and @AdaTheDev mentioned, this will only work in newer versions (>=2008).

与@MicSim和@AdaTheDev一样,这只适用于较新的版本(> = 2008)。