Wrong week numbers

  • Please can someone tell me why this resul;ts from the below code produce an incorrect week number?!?!?!

     

    ----------------------------------------------------------------

    set nocount on

    --set DATEFIRST 1

    DECLARE @counter int

    set @counter = 0

    declare @looper datetime

    set @looper = '20041225'

    WHILE @counter < 15

    BEGIN

    PRINT CAST(@looper as varchar(11)) + ' - week ' + CAST(DATEPART(wk,@looper) as varchar)

    set @looper = DATEADD(d,1,@looper)

    set @counter = @counter + 1

    END

    -----------------------------------------------------------------------

  • Hi Ben,

    This is from Books OnLine:

    The week (wk, ww) datepart reflects changes made to SET DATEFIRST. January 1 of any year defines the starting number for the week datepart, for example: DATEPART(wk, 'Jan 1, xxxx') = 1, where xxxx is any year.

     

    Regards,
    Leon Bakkers

  • You might also have a look at BOL at "CREATE FUNCTION" and the example ISOWeek given there.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • The function is returning the correct week number as 53 due to its use of a standard Sunday to Saturday week.  In this case Jan 1 - Jan 3 2004 is week one, Jan 4 - 10 is week two, etc.  Therefore Dec 19 - 25 is week 52 and Dec 26 - Dec 31 2004 is week 53,

    Andrew

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply