locked
53 weeks in a year RRS feed

  • Question

  • Hi all,

    Does anybody know how to calculate if a year has 52 or 53 weeks in a year in sql 2008?

    Thanks

    Mili
    Monday, March 30, 2009 2:17 PM

Answers

  • Well, there's something i didn't know. I always thought there were 52 in a year....

    But

    select datediff(wk, '31 dec 2005', '31 dec 2006')


    every day is a school day
    • Marked as answer by Milli_22 Sunday, April 5, 2009 9:01 PM
    Monday, March 30, 2009 2:55 PM
  • Tried this?

    SELECT

    DATEDIFF(WK,'2000-1-1','2000-12-31')
    SELECT DATEDIFF(WK,'2005-1-1','2005-12-31')
    SELECT DATEDIFF(WK,'2008-1-1','2008-12-31')

    • Marked as answer by Milli_22 Sunday, April 5, 2009 9:01 PM
    Monday, March 30, 2009 3:12 PM

All replies

  • The week number of Dec 31 ought to have that: SELECT DATEPART(wk, '2008-12-31')
    Monday, March 30, 2009 2:32 PM
  • But that gives 53 weeks everytime i do it

    ie

    SELECT DATEPART(wk, '2008-12-31')
    SELECT DATEPART(wk, '2009-12-31')
    SELECT DATEPART(wk, '2010-12-31')
    SELECT DATEPART(wk, '2011-12-31')

    53 weeks in a year only happen i think every 7 or so years...

    Mili
    Monday, March 30, 2009 2:46 PM
  • Well, there's something i didn't know. I always thought there were 52 in a year....

    But

    select datediff(wk, '31 dec 2005', '31 dec 2006')


    every day is a school day
    • Marked as answer by Milli_22 Sunday, April 5, 2009 9:01 PM
    Monday, March 30, 2009 2:55 PM
  • Tried this?

    SELECT

    DATEDIFF(WK,'2000-1-1','2000-12-31')
    SELECT DATEDIFF(WK,'2005-1-1','2005-12-31')
    SELECT DATEDIFF(WK,'2008-1-1','2008-12-31')

    • Marked as answer by Milli_22 Sunday, April 5, 2009 9:01 PM
    Monday, March 30, 2009 3:12 PM
  • How do you define the difference between 52 and 53 weeks?
    Monday, March 30, 2009 3:17 PM
  • What do you mean by 53 weeks. Every year is 52 weeks and 1 day (2 days if a leap year) so every year will span at least 53 weeks. 
    When jan 1st is on A Sat. (or Fri and a leap year) the year will span 54 weeks

    or so i think

    Monday, March 30, 2009 3:18 PM
  • I'm second to Wisepin.

    Every year (regardless it is leap or not) has 52 complete week. 
    With datepart function you will get 53 or 54 (as pointed out by wisepin jan 1st is on A Sat or fri with leap year.. e.g Year 2000) as it is just Week number of year, is not a indicator whether week is full or not.



    Mangal Pardeshi
    SQL With Mangal

    Monday, March 30, 2009 3:56 PM
  • This is the correct way to calculate it:
    ALTER function fnWeeksInYear(@Yr as int) returns int AS
    BEGIN
     Return (Select DateDiff(WK, Cast(@yr as varchar(4))+'-1-1', Cast(@yr+1 as varchar(4))+'-1-1'))
    END
    
    thilla's is almost correct, but suffers from fencepost error: the correct range to count for the year 2000 is '2000-1-1' to '2001-1-1'.
    RBarryYoung
    • Proposed as answer by RBarryYoung Monday, March 30, 2009 11:46 PM
    Monday, March 30, 2009 11:46 PM