Solved

Update Table with Fiscal week Numbers for the fiscal Year(s)

Posted on 2008-06-24
10
887 Views
Last Modified: 2011-09-20
Hello -
We have a table that looks as follows:
fiscalweekstartdate fiscalweekenddate       fiscalweeknumber fiscalmonthnumber      fiscalyearnumber
2007-06-30 00:00:00      2007-07-06 23:59:00      1      1      2008
2007-07-07 00:00:00      2007-07-13 23:59:00      2      1      2008
2007-07-14 00:00:00      2007-07-20 23:59:00      3      1      2008
2007-07-21 00:00:00      2007-07-27 23:59:00      4      1      2008

Our fiscal year starts in July, and ends in June.   I would like to update the table by insertings values for each fiscal week based on the week start and end dates. Each new fiscal week is a new number. Each fiscal year has 52 or 53 fiscal weeks, and then starts over with a new fiscal year as indicated by the fiscal year on the right. I need to know how to insert these fiscal week # values for the whole table. Thanks!
0
Comment
Question by:savetheday
  • 5
  • 5
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
0
 

Author Comment

by:savetheday
Comment Utility
I already read that. What I need now is a way to fill in the fiscalweek numbers in the table based on data like above.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Are your fiscalweekstartdate & fiscalweekenddate dates already populated?  ie, are you going to be updating rows or inserting them?
0
 

Author Comment

by:savetheday
Comment Utility
the fiscalweekstartdate and enddates are already populated. From those I would derive the fiscal week number (e.g. 1 - 52) and start over in a new year (e.g. 2009). the table pretty much has everyting I need except the fiscal week number.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
try this...this will loop through them and update them accordingly.
declare @fiscalweek int, @startdate datetime

set @fiscalweek = 1
 

DECLARE  CursorTemplate CURSOR 

FAST_FORWARD FOR 	

	SELECT fiscalweekstartdate FROM yourtablename

	where fiscalweekstartdate >= '2007-06-30 00:00:00'
 

OPEN CursorTemplate
 

FETCH NEXT FROM CursorTemplate 

INTO	@startdate
 

WHILE (@@FETCH_STATUS = 0)

BEGIN
 

	update yourtablename

	set fiscalweeknumber = @fiscalweek

	where fiscalweekstartdate = @startdate
 

	set @fiscalweek = @fiscalweek + 1
 
 

	FETCH NEXT FROM CursorTemplate 

	INTO	@startdate
 

END
 

CLOSE CursorTemplate

DEALLOCATE CursorTemplate

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:savetheday
Comment Utility
Hello ChapManDew:

This loops through and adds one for each fiscal week, but keeps going w/out resetting when a new fiscal year starts:
This was done when I put the date as the start of the table (1989-07-01 00:00:00).

fiscalweekstartdate fiscalweekenddate      fiscalweeknumberfiscalmonthnumber      fiscalyearnumber
2007-06-30 00:00:00      2007-07-06 23:59:00      19      1      2008
2007-07-07 00:00:00      2007-07-13 23:59:00      377      1      2008
2007-07-14 00:00:00      2007-07-20 23:59:00      735      1      2008
2007-07-21 00:00:00      2007-07-27 23:59:00      1093      1      2008
2007-07-28 00:00:00      2007-08-03 23:59:00      50      2      2008
2007-08-04 00:00:00      2007-08-10 23:59:00      408      2      2008

S/B fiscal week 1,2,3,4,5,6 etc. until June 28, 2008 where a new fiscal year starts and fiscal week becomes 1 again.

Can you do that? Thanks -
Savetheday
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Will the date be the same every year (with a new  year number)?
0
 

Author Comment

by:savetheday
Comment Utility
not necessarily. The last day of the fiscal week is always Friday, so Sat. starts the new fiscal week, and the new year. This year is Jun 28. But all of the dates are already in the table by start and end date for each week. All I need to do is put in the fiscal week #, and start over each new fiscal year (which is also in the table, e.g. 2008, 2009, 2010). So 6/21/08-6/27/08 is fiscal week # 52 in 2008, and 6/28/08 - 7/4/08 is fiscal week # 1 for 2009:


fiscalweekstartdatefiscalweekenddate      fiscalweeknumberfiscalmonthnumber      fiscalyearnumber
2008-06-21 00:00:00      2008-06-27 23:59:00      1544      12      2008
2008-06-28 00:00:00      2008-07-04 23:59:00      20      1      2009
2008-07-05 00:00:00      2008-07-11 23:59:00      378      1      2009
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
Comment Utility
Hmm...in that case use this code instead.  You'll need to plug in the dates for each year and run them one at a time for each year, but it should be easy enough:


declare @fiscalweek int, @startdate datetime

set @fiscalweek = 1

 

DECLARE  CursorTemplate CURSOR 

FAST_FORWARD FOR 	
 

	SELECT fiscalweekstartdate FROM yourtablename

	where 

		fiscalweekstartdate >= '2007-06-30 00:00:00' and 

		fiscalweekstartdate <= '2008-06-30 00:00:00'

	order by fiscalweekstartdate asc

  

FETCH NEXT FROM CursorTemplate 

INTO	@startdate

 

WHILE (@@FETCH_STATUS = 0)

BEGIN

 

	update yourtablename

	set fiscalweeknumber = @fiscalweek

	where fiscalweekstartdate = @startdate

 

	set @fiscalweek = @fiscalweek + 1

 

	FETCH NEXT FROM CursorTemplate 

	INTO	@startdate

 

END

 

CLOSE CursorTemplate

DEALLOCATE CursorTemplate

Open in new window

0
 

Author Comment

by:savetheday
Comment Utility
I just had to add OPEN CursorTemplate and the code works great. I was thinking this would need to be entered one year at a time - but well worth it to find a solution.

Great solution!!  One of the best I have had yet.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now