Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to get week number and startday of the week in sql server 2008

how to get week number and startday of the week in sql server 2008

Scheduled Pinned Locked Moved Database
helptutorialdatabasesql-serversysadmin
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chakran
    wrote on last edited by
    #1

    Hi, I am struggling to get the week number and week day of the particular day. I know we can use Datepart function which is available in sql server 2008 to display the week number of the given date. but my situation is different. for example Datepart(wk,'2009-12-30') gives 53 weeks Datepart(wk,'2010-01-02') gives 1. but I wanted to display it as 54. whenever the year is changing I need to add these week number to the previous year one. In the below table Bugdate is the input, and weeknumber and startday of the week are the expected outputs. BugDate ----- weeknumber ---- Startday of the Week .... 12/13/2008---- 50------------- 12/7/2008 12/14/2008---- 51------------- 12/14/2008 12/21/2008---- 52------------- 12/21/2008 12/23/2008---- 52------------- 12/21/2008 12/30/2008---- 53------------- 12/28/2008 1/2/2009------- 54------------- 12/28/2008 1/6/2009------- 55------------- 1/4/2009 ... 12/20/2009---- 105------------ 12/20/2009 12/28/2009---- 106------------ 12/27/2009 1/1/2010------ 106------------ 12/27/2009 Can any one help me to solve this problem. Thanks in advance.

    M N 2 Replies Last reply
    0
    • C chakran

      Hi, I am struggling to get the week number and week day of the particular day. I know we can use Datepart function which is available in sql server 2008 to display the week number of the given date. but my situation is different. for example Datepart(wk,'2009-12-30') gives 53 weeks Datepart(wk,'2010-01-02') gives 1. but I wanted to display it as 54. whenever the year is changing I need to add these week number to the previous year one. In the below table Bugdate is the input, and weeknumber and startday of the week are the expected outputs. BugDate ----- weeknumber ---- Startday of the Week .... 12/13/2008---- 50------------- 12/7/2008 12/14/2008---- 51------------- 12/14/2008 12/21/2008---- 52------------- 12/21/2008 12/23/2008---- 52------------- 12/21/2008 12/30/2008---- 53------------- 12/28/2008 1/2/2009------- 54------------- 12/28/2008 1/6/2009------- 55------------- 1/4/2009 ... 12/20/2009---- 105------------ 12/20/2009 12/28/2009---- 106------------ 12/27/2009 1/1/2010------ 106------------ 12/27/2009 Can any one help me to solve this problem. Thanks in advance.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You could do something like Select Datepart(wk,'2010-01-02') + ((Datepart(yr,'2010-01-02') - 2009) * 52 OR get the datediff days / 7 and use floor or ceiling to get the number you want (this is more reliable as the week numbers per year do not change. This is a fairly simple exercise of thinking around corners, you are not flexing you brain enough.

      Never underestimate the power of human stupidity RAH

      C 2 Replies Last reply
      0
      • M Mycroft Holmes

        You could do something like Select Datepart(wk,'2010-01-02') + ((Datepart(yr,'2010-01-02') - 2009) * 52 OR get the datediff days / 7 and use floor or ceiling to get the number you want (this is more reliable as the week numbers per year do not change. This is a fairly simple exercise of thinking around corners, you are not flexing you brain enough.

        Never underestimate the power of human stupidity RAH

        C Offline
        C Offline
        chakran
        wrote on last edited by
        #3

        Hi, Thanks for your reply to get the week number. But how to get the first day of the week for the given date? Thanks,

        M 1 Reply Last reply
        0
        • C chakran

          Hi, Thanks for your reply to get the week number. But how to get the first day of the week for the given date? Thanks,

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Read up on datepart in BOL, there is a dayofweek element that will tell you.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • C chakran

            Hi, I am struggling to get the week number and week day of the particular day. I know we can use Datepart function which is available in sql server 2008 to display the week number of the given date. but my situation is different. for example Datepart(wk,'2009-12-30') gives 53 weeks Datepart(wk,'2010-01-02') gives 1. but I wanted to display it as 54. whenever the year is changing I need to add these week number to the previous year one. In the below table Bugdate is the input, and weeknumber and startday of the week are the expected outputs. BugDate ----- weeknumber ---- Startday of the Week .... 12/13/2008---- 50------------- 12/7/2008 12/14/2008---- 51------------- 12/14/2008 12/21/2008---- 52------------- 12/21/2008 12/23/2008---- 52------------- 12/21/2008 12/30/2008---- 53------------- 12/28/2008 1/2/2009------- 54------------- 12/28/2008 1/6/2009------- 55------------- 1/4/2009 ... 12/20/2009---- 105------------ 12/20/2009 12/28/2009---- 106------------ 12/27/2009 1/1/2010------ 106------------ 12/27/2009 Can any one help me to solve this problem. Thanks in advance.

            N Offline
            N Offline
            Niladri_Biswas
            wrote on last edited by
            #5

            Try this . declare @tbl table(bugdate date) insert into @tbl select '12/13/2008' union all select '12/14/2008' union all select '12/21/2008' union all select '12/23/2008' union all select '12/30/2008' union all select '1/2/2009' union all select '1/6/2009' union all select '12/20/2009' union all select '12/28/2009' union all select '1/1/2010'

            select bugdate,
            case when Datepart(yy,bugdate) = 2009 then Datepart(wk,bugdate) + 53*1
            when Datepart(yy,bugdate) = 2010 then Datepart(wk,bugdate) + 53*2
            else Datepart(wk,bugdate)
            end as weeknumber
            ,DATEPART(dw,bugdate) weekdays
            ,DATENAME(dw, DATEPART(dw,bugdate)) weekdayname
            ,DATEadd(day, (1- DATEPART(dw,bugdate)), bugdate) AS startweek
            ,DATENAME(dw, DATEadd(day, (1- DATEPART(dw,bugdate)), bugdate)) startweekdayname
            from @tbl

            Output:

            bugdate weeknumber weekdays weekdayname startweek startweekdayname
            2008-12-13 50 7 Monday 2008-12-07 Sunday
            2008-12-14 51 1 Tuesday 2008-12-14 Sunday
            2008-12-21 52 1 Tuesday 2008-12-21 Sunday
            2008-12-23 52 3 Thursday 2008-12-21 Sunday
            2008-12-30 53 3 Thursday 2008-12-28 Sunday
            2009-01-02 54 6 Sunday 2008-12-28 Sunday
            2009-01-06 55 3 Thursday 2009-01-04 Sunday
            2009-12-20 105 1 Tuesday 2009-12-20 Sunday
            2009-12-28 106 2 Wednesday 2009-12-27 Sunday
            2010-01-01 107 6 Sunday 2009-12-27 Sunday

            Depending on ur requirement, accept the columns. Note that I have multiplied 53 * 1 for 2009 & 53*2 for 2010. Now why 53? Because if you want to find out the last week of the last day of a year it will be 53. Try this Select Datepart(wk,'12/31/2008') or Select Datepart(wk,'12/31/2009') etc. The output will be 53. I am considering 2008(here) as my starting point and henceforth nothing to add with the weeks for this year. Since year 2009 is 1 ahead so I am multiplying 53 with 1(which is though of no use) and adding that to every week of that year(2009). The same rule apply for 2010 with the difference that 53 will be multiplied with 2. If we follow this rule then we can generalized a formula 53 * n where n=0,1.... Note- Follow what Mr.Mycroft said. It is better for ur learning. :)

            Niladri Biswas

            modified on Friday, October 30, 2009 5:03 AM

            1 Reply Last reply
            0
            • M Mycroft Holmes

              You could do something like Select Datepart(wk,'2010-01-02') + ((Datepart(yr,'2010-01-02') - 2009) * 52 OR get the datediff days / 7 and use floor or ceiling to get the number you want (this is more reliable as the week numbers per year do not change. This is a fairly simple exercise of thinking around corners, you are not flexing you brain enough.

              Never underestimate the power of human stupidity RAH

              C Offline
              C Offline
              chakran
              wrote on last edited by
              #6

              Hi, the query is failing in one situation. eg, when dates are '2011-12-31' '2012-01-01', this case returning same week number for both the dates but this is wrong. For '2012-01-01' this is the start of the week so it is increment of the previous date week... Date-------- weekNumber 2011-12-31-- 157 2012-01-01-- 157(wrong result it should be 158) 2012-01-02-- 157(wrong result it should be 158) Thanks..

              N 1 Reply Last reply
              0
              • C chakran

                Hi, the query is failing in one situation. eg, when dates are '2011-12-31' '2012-01-01', this case returning same week number for both the dates but this is wrong. For '2012-01-01' this is the start of the week so it is increment of the previous date week... Date-------- weekNumber 2011-12-31-- 157 2012-01-01-- 157(wrong result it should be 158) 2012-01-02-- 157(wrong result it should be 158) Thanks..

                N Offline
                N Offline
                Niladri_Biswas
                wrote on last edited by
                #7

                But as per the query which I have given it should be Date-------- weekNumber 2011-12-31-- 212 and not 157 2012-01-01-- 213 and not 157 or 158 2012-01-02-- 213 and not 157 or 158 And it is correct only. Even I calculated the value. Only change the case statement by the following lines

                case when Datepart(yy,bugdate) = 2009 then Datepart(wk,bugdate) + 53*1
                when Datepart(yy,bugdate) = 2010 then Datepart(wk,bugdate) + 53*2
                when Datepart(yy,bugdate) = 2011 then Datepart(wk,bugdate) + 53*3
                when Datepart(yy,bugdate) = 2012 then Datepart(wk,bugdate) + 53*4
                else Datepart(wk,bugdate)
                end as weeknumber

                :)

                Niladri Biswas

                C 1 Reply Last reply
                0
                • N Niladri_Biswas

                  But as per the query which I have given it should be Date-------- weekNumber 2011-12-31-- 212 and not 157 2012-01-01-- 213 and not 157 or 158 2012-01-02-- 213 and not 157 or 158 And it is correct only. Even I calculated the value. Only change the case statement by the following lines

                  case when Datepart(yy,bugdate) = 2009 then Datepart(wk,bugdate) + 53*1
                  when Datepart(yy,bugdate) = 2010 then Datepart(wk,bugdate) + 53*2
                  when Datepart(yy,bugdate) = 2011 then Datepart(wk,bugdate) + 53*3
                  when Datepart(yy,bugdate) = 2012 then Datepart(wk,bugdate) + 53*4
                  else Datepart(wk,bugdate)
                  end as weeknumber

                  :)

                  Niladri Biswas

                  C Offline
                  C Offline
                  chakran
                  wrote on last edited by
                  #8

                  the problem with your query is if the years will be increased in the future. so instead of adding condition many times we need to summarize that. second is --Date----- weeknumber 2008-12-30--- 53 2009-01-02--- 54( this is wrong it should be 53, it falls in the 53rd week) Thanks,

                  N 1 Reply Last reply
                  0
                  • C chakran

                    the problem with your query is if the years will be increased in the future. so instead of adding condition many times we need to summarize that. second is --Date----- weeknumber 2008-12-30--- 53 2009-01-02--- 54( this is wrong it should be 53, it falls in the 53rd week) Thanks,

                    N Offline
                    N Offline
                    Niladri_Biswas
                    wrote on last edited by
                    #9

                    Come on. it is correct. Because if you execute select Datepart(wk,'12/30/2008') you will get the weeknumber as 53 and select Datepart(wk,'1/2/2009') will yield 1. As per your requirement, which you specified it should be 54. and henceforth the result. For the first part(if the years will be increased in the future. so instead of adding condition many times we need to summarize that) we need to work on. :)

                    Niladri Biswas

                    C 1 Reply Last reply
                    0
                    • N Niladri_Biswas

                      Come on. it is correct. Because if you execute select Datepart(wk,'12/30/2008') you will get the weeknumber as 53 and select Datepart(wk,'1/2/2009') will yield 1. As per your requirement, which you specified it should be 54. and henceforth the result. For the first part(if the years will be increased in the future. so instead of adding condition many times we need to summarize that) we need to work on. :)

                      Niladri Biswas

                      C Offline
                      C Offline
                      chakran
                      wrote on last edited by
                      #10

                      sorry,I am wrong, the out should be 53 not 54. because 1/2/2009 falls in week 53. Is it possible to modify the query to meet my requirement... The reply given by 'Mycroft Holmes' is working perfect but there is a small problem in that query, I posted the same in earlier thread. Thanks,

                      N 1 Reply Last reply
                      0
                      • C chakran

                        sorry,I am wrong, the out should be 53 not 54. because 1/2/2009 falls in week 53. Is it possible to modify the query to meet my requirement... The reply given by 'Mycroft Holmes' is working perfect but there is a small problem in that query, I posted the same in earlier thread. Thanks,

                        N Offline
                        N Offline
                        Niladri_Biswas
                        wrote on last edited by
                        #11

                        Please provide a sample output which should be correct enough. Otherwise it is becoming difficult for me to understand ur requirement. Also u give a shot. As I told u that u can learn better if u try by urself. :)

                        Niladri Biswas

                        C 1 Reply Last reply
                        0
                        • N Niladri_Biswas

                          Please provide a sample output which should be correct enough. Otherwise it is becoming difficult for me to understand ur requirement. Also u give a shot. As I told u that u can learn better if u try by urself. :)

                          Niladri Biswas

                          C Offline
                          C Offline
                          chakran
                          wrote on last edited by
                          #12

                          Hi, Bugdate is the input and startweek, weeknumber are expected output. when date is 2009-01-02     weeknumber should be 53 and when date is 2012-01-01 week number should be 210. BugDate              Startweek      WeekNumber ---------------------------------------- 2008-12-13     2008-12-07     50 2008-12-14     2008-12-14     51 2008-12-21     2008-12-21     52 2008-12-23     2008-12-21     52 2008-12-30     2008-12-28     53 2009-01-02     2008-12-28     53 2009-12-20     2009-12-20     104 2009-12-28     2009-12-27     105 2010-01-01     2009-12-27     105 2011-12-31     2011-12-25     209 2012-01-01     2012-01-01     210 2012-01-02     2012-01-01     210 2012-01-09     2012-01-08     211

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups