Solved

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

Posted on 2008-06-24
10
894 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 500 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

867 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

12 Experts available now in Live!

Get 1:1 Help Now