Leap Year got Me ... Help!

  • On 1 January we were trying to pull in our normal 7 day load (7 Days) and it pulled one too may days.  We believe it is due to leap year last year.   What did I do wrong?  Can someone give me a good set of code for leap year?

    declare @i_to_day int

    declare @i_to_year int

    declare @str_to_day varchar(3)

    declare @str_to_year varchar(2)

    select @current_max =  max(dateprocessed) from table1

    ---- PULL JULIAN DATE ... YY, DDD

    select @i_to_year = convert(int,substring(@current_max,2,2))

    select @i_to_day = convert(int,substring(@current_max,4,3))

    select @i_to_day = @i_to_day + 8

    if @i_to_day > 365  -- means that the start day was in the previous year if we are adding 7 days

    begin

     select @i_to_year = @i_to_year + 1

     select @i_to_day = @i_to_day % 365

    end

  • IF ISDATE(CAST(DATEPART(YEAR, GETDATE()) AS CHAR(4)) + '-02-29') = 1

      BEGIN

        PRINT 'Leap year'

      END

    ELSE

      BEGIN

        PRINT 'Not a leap year'

      END



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

  • We work with a date range so need to go by julian date ... can I say if it is 366 only add 7 days rather than 8?

  • I'm sure you can.  The code sample I provided is just 1 way of attempting to ID if the year is a leap year or not.  The meat of the code would be where you add 7 OR 8...



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

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

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