Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-24
10
Medium Priority
?
932 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
ID: 21858931
0
 

Author Comment

by:savetheday
ID: 21859270
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
ID: 21859290
Are your fiscalweekstartdate & fiscalweekenddate dates already populated?  ie, are you going to be updating rows or inserting them?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:savetheday
ID: 21859331
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
ID: 21859477
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
 

Author Comment

by:savetheday
ID: 21860478
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
ID: 21860621
Will the date be the same every year (with a new  year number)?
0
 

Author Comment

by:savetheday
ID: 21860959
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 2000 total points
ID: 21861401
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
ID: 21862406
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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

972 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