Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get cummulative week number

Author  Topic 

chakrinn
Starting Member

3 Posts

Posted - 2009-10-28 : 08:07:49
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. How to generated it dynamically.

Date ------ weekNumber
----------------------------
12/21/2009 --> 52
12/23/2009 --> 52
12/30/2009 --> 53
1/2/2010 --> 1 Here I wanted to display as 54....

Thanks in advance

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-28 : 08:55:56
Like this

select datepart(wk,'1/2/2010 ')+case when year('1/2/2010')<>year(getdate()) then datepart(wk,'12/31/'+datename(year,getdate())) else 0 end

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

chakrinn
Starting Member

3 Posts

Posted - 2009-10-28 : 09:11:21
Thanks Mashivanan. How can we get the first day of the week in the same query. say in the above example 12/21/2009 returns week number as 52, the start day for this week is 12/20/2009. and for 1/2/2010 it returns week number as 54 according to your query, and need to display the starting date for this week is 12/27/2009. How can we do this.
Thanks..
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-10-28 : 11:55:27
seems you want DATEDIFF, not DATEPART
Go to Top of Page

chakrinn
Starting Member

3 Posts

Posted - 2009-10-29 : 03:17:25
what ever function we use but finally need the output of how I am requested..
Go to Top of Page
   

- Advertisement -