Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Testing for holidays, part 2
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Testing for holidays, part 2

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: 17 September 2003
I saw your previous solution for counting the number of work days. How can I modify it to not count dates in my holiday table?
CREATE TABLE  holiday
(
    id         NUMBER (10)    PRIMARY KEY,
    start_dt   DATE           NOT NULL,
    end_dt     DATE           NOT NULL,
    cmnt       VARCHAR2 (100)
);

I've attached some sample data for the table.


>

The following is continued from part 1.

Code for holiday_num Function

When comparing dates in Oracle, always remember that they contain a time component. When you ask "Is SYSDATE + 21 a holiday?", you may think you're asking "Is October 13 a holiday?", but actually you're asking "Is October 13, 1:08 p.m., a holiday?". Is the difference significant? It is if you inserted '13-Oct-2003' as the end of the holiday, because Oracle interprets that as being 12 midnight at the beginning of October 13, which is before 1:08 in the afternoon of the same day. Use the TRUNC function if you want to ignore the time of day.

--  holiday_num returns a positive number if in_dt is
--  a holiday according to the holiday table (that is,
--  if in_dt falls between start_dt and end_dt (inclusive)
--  for some row in holiday.
--    The number returned is the id for that row (or for
--  an arbitrary one of the rows, if there are several.)
--    If in_dt is not a holiday, 0 is returned.  (Therefore,
--  don't use 0 as an id.)

CREATE OR REPLACE FUNCTION holiday_num
(
    in_dt  DATE
)
RETURN  NUMBER
IS
    first_id  holiday.id%TYPE;
BEGIN
    SELECT  id
     INTO   first_id
     FROM   holiday
     WHERE  TRUNC (in_dt) BETWEEN TRUNC (start_dt)
                              AND (end_dt)
       AND  ROWNUM = 1;

    RETURN  first_id;    -- It is a holiday
EXCEPTION
    WHEN  NO_DATA_FOUND
        THEN  RETURN 0;  -- Not a holiday
    WHEN  OTHERS
        THEN  RETURN NULL;
END  holiday_num;

Sample Data

INSERT INTO  holiday
(
    id,
    start_dt,
    end_dt,
    cmnt
) 
VALUES
(
    holiday_id_seq.NEXTVAL,
    '13-Oct-2003',
    '13-Oct-2003',
    'Columbus Day'
);

INSERT INTO  holiday
(
    id,
    start_dt,
    end_dt,
    cmnt
) 
VALUES
(
    holiday_id_seq.NEXTVAL,
    '27-Nov-2003',
    '28-Nov-2003',
    'Thanksgiving (Thursday); business closed Friday, too'
);


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