Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2008-06-24
Medium Priority
928 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
Question by:savetheday
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 5
• 5
10 Comments

LVL 60

Expert Comment

ID: 21858931
0

Author Comment

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

ID: 21859290
Are your fiscalweekstartdate & fiscalweekenddate dates already populated?  ie, are you going to be updating rows or inserting them?
0

Author Comment

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

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
``````
0

Author Comment

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

ID: 21860621
Will the date be the same every year (with a new  year number)?
0

Author Comment

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

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
``````
0

Author Comment

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

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages â€“ plus untold reputational damage to one of the worldâ€™s most trusted airlines. All due to a catastrophâ€¦
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE sâ€¦
###### Suggested Courses
Course of the Month8 days, 19 hours left to enroll

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

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