dbTalk Databases Forums  

Creating a list of every week of the year

microsoft.public.sqlserver.olap microsoft.public.sqlserver.olap


Discuss Creating a list of every week of the year in the microsoft.public.sqlserver.olap forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Krule
 
Posts: n/a

Default Creating a list of every week of the year - 10-20-2003 , 07:54 PM







I'm attempting to create some sales statistics based on weekly sales.



I've got my sales categorized by weeks, and I want to join it to a table
that has every week #, as well as the date of the monday that this
particular week falls on.



SO like

Week # Date

1 Jan-1-2003

2 Jan-8-2003



And so on and so forth. Any ideas?

(I want to use this table to outer join my table of sales it's getting
to be a headache)


--
Posted via http://dbforums.com

Reply With Quote
  #2  
Old   
malcolm k
 
Posts: n/a

Default Re: Creating a list of every week of the year - 10-21-2003 , 09:44 AM







Something like the following - change the select to an insert

--
Declare @YearStart datetime
Declare @WeekStart int
Declare @MyDate datetime
Set @YearStart = '01/01/2003'
Set @WeekStart = 1
--
select @MyDate =
case DatePart(weekday,@YearStart)
when 1 then DateAdd(day,1,@YearStart)
when 2 then @YearStart
else
dateAdd(day,9-DatePart(weekday,@YearStart),@YearStart)
end

-- insert into table
While Datepart(year,@MyDate)<YEAR(@YearStart)+1
Begin
select @WeekStart,Convert(nvarchar(12),@MyDate,109)
set @WeekStart=@Weekstart+1
set @Mydate = DateAdd(day,7,@MyDate)
end
--


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.