Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Fiscal year, ISO week, part 1
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Fiscal year, ISO week, part 1

Frank Kulash EXPERT RESPONSE FROM: Frank Kulash

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 30 September 2003
I need to get the ISO week number based upon our fiscal year rather than calendar year. Our fiscal year begins on September 1 each year and the corresponding 'IW' can be 52, 53 or 1.

>
Saving a more general definition for later, let's say that every week starts on Sunday and ends the following Saturday. The fiscal year begins on the Sunday closest to September 1, which is equivalent to the Sunday following August 28. Given a date and the date of the corresponding fiscal New Year's Day, computing the ISO week is just a matter of counting the days between them and dividing by seven. If the resulting number (disregarding any fractional part) is n, the week is n+1. The trickiest part is determining what is "corresponding fiscal New Year's Day," because, as you pointed out, September 1 may be in week one of the new year, or it may be in week 52 or 53 of the old year. (The same goes for all the dates between August 29 and September 3.)

The function fiscal_week, below, returns the value you're looking for.

CREATE OR REPLACE FUNCTION  fiscal_new_year

--  *************************************
--  **  f i s c a l _ n e w _ y e a r  **
--  *************************************

--  fiscal_new_year returns the date of the
--  start of the most recent fiscal year on
--  or before in_dt.

--  FUNCTION  fiscal_new_year
(
    in_dt  IN  DATE
)
RETURN  DATE
DETERMINISTIC
AS
    fny_dt     DATE;  -- candidate for being returned
    year_text  CHAR (4) := TO_CHAR (in_dt, 'YYYY');
BEGIN
    -- fny may be in the same calendar year or the previous one
    FOR try_year IN 1 .. 2
    LOOP
        -- fny is the first Sunday after August 28
        fny_dt := NEXT_DAY ( TO_DATE ( year_text || '0828',
                                       'YYYYMMDD'
                                     ),
                             'SUNDAY' -- First day of the week
                           );
        IF  fny_dt <= in_dt
        THEN  -- This guess was correct
            EXIT;
        END IF;

        -- in_dt is before fny_dt; get set to try previous year
        year_text := TRIM ( TO_CHAR ( TO_NUMBER ( year_text,
                                                  '9999'
                                                ) - 1,
                                      '9999'
                                    )
                          );
    END LOOP;

    RETURN  fny_dt;
END  fiscal_new_year;
/

CREATE OR REPLACE FUNCTION  fiscal_week

--  *****************************
--  **  f i s c a l _ w e e k  **
--  *****************************

--  fiscal_week returns an integer (1-53) indicating
--  which week of the fiscal year in_dt is in.

--  FUNCTION  fiscal_week
(
    in_dt  IN  DATE
)
RETURN  PLS_INTEGER
DETERMINISTIC
AS
BEGIN
    RETURN  1 + FLOOR ((in_dt - fiscal_new_year (in_dt)) / 7);
END  fiscal_week;
/

Continued in part 2.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts