473,511 Members | 9,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function To Calculate Business Days

debasisdas
8,127 Recognized Expert Expert
This function takes 2 dates as parameter and returns the number of working days. You need to add the list of holidays. (I have added a few as sample)


Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION BUSINESS_DAYS (
  2.   i_Date1 IN DATE,
  3.   i_Date2 IN DATE
  4.   )
  5. RETURN NUMBER
  6. IS
  7.   V_LEAST          DATE := TRUNC(LEAST(i_Date1, i_Date2));
  8.   V_GREATEST       DATE := TRUNC(GREATEST(i_Date1, i_Date2));
  9.   V_RESULT         NUMBER;
  10.  
  11.   V_FIRST_DOWK     NUMBER;
  12.   V_LAST_DOWK      NUMBER;
  13.   V_WKS_BTWN       NUMBER;
  14.   V_SAT_ADJST      NUMBER := 0;
  15.   V_COUNT_FIRST    NUMBER := 1;
  16.   V_PLS            PLS_INTEGER := 1;
  17. BEGIN
  18.  
  19.   V_FIRST_DOWK := TO_NUMBER( TO_CHAR(V_LEAST, 'D' ) );
  20.   V_LAST_DOWK := TO_NUMBER( TO_CHAR(V_GREATEST, 'D' ) );
  21.   V_WKS_BTWN := TRUNC( (V_GREATEST - V_LEAST ) / 7 );
  22.  
  23.   IF V_FIRST_DOWK > V_LAST_DOWK
  24.   THEN
  25.      V_WKS_BTWN := V_WKS_BTWN + 1;
  26.   END IF;
  27.  
  28.   IF V_FIRST_DOWK = 7
  29.   THEN
  30.     V_SAT_ADJST := 1;
  31.     V_COUNT_FIRST := 0;
  32.   ELSIF V_FIRST_DOWK = 1
  33.   THEN
  34.     V_COUNT_FIRST := 0;
  35.   ELSE
  36.      NULL;
  37.   END IF;
  38.  
  39.   IF V_LAST_DOWK = 7
  40.   THEN
  41.      V_SAT_ADJST := V_SAT_ADJST - 1;
  42.   END IF;
  43.  
  44.   V_RESULT := ((V_WKS_BTWN * 5 ) + (V_LAST_DOWK - V_FIRST_DOWK + V_COUNT_FIRST) + V_SAT_ADJST) * V_PLS;
  45.  
  46.   IF V_RESULT > 0
  47.   THEN
  48.  --FOR 2007 
  49.  
  50.  --FOR 2008 
  51.  
  52.  --FOR 2009
  53.  
  54.  IF TO_DATE('01-01-2009','DD-MM-YYYY') BETWEEN i_Date1 AND i_Date2
  55.     THEN
  56.       V_RESULT := V_RESULT - 1;
  57.     END IF;
  58.  
  59.  IF TO_DATE('19-03-2009','DD-MM-YYYY') BETWEEN i_Date1 AND i_Date2
  60.     THEN
  61.       V_RESULT := V_RESULT - 1;
  62.     END IF;
  63.  
  64.  IF TO_DATE('15-07-2009','DD-MM-YYYY') BETWEEN i_Date1 AND i_Date2
  65.     THEN
  66.       V_RESULT := V_RESULT - 1;
  67.     END IF;
  68. ----------------------------------------------------------
  69. ------------------add more dates here-----------
  70. ----------------------------------------------------------
  71.  
  72.   RETURN V_RESULT;
  73. /*
  74. EXCEPTION
  75.   WHEN OTHERS
  76.   THEN
  77.     V_ERROR := TO_CHAR(SQLERRM);
  78. --    INSERT INTO CHARAN(NAME, Dt)
  79. --    VALUES(V_ERROR || ' - ' || i_Date1 || ' & ' || i_Date2, SYSDATE);
  80. --    COMMIT;
  81. --    RAISE_APPLICATION_ERROR('-20001', 'Oops. Invalid Date format. It should be "DD-MON-YYYY"');
  82.     RAISE_APPLICATION_ERROR('-20001', TO_CHAR(SQLERRM) || ' - ' || i_Date1 || ' & ' || i_Date2);
  83. */
  84. END;
  85.  
  86.  
Jun 30 '09 #1
0 9206

Sign in to post your reply or Sign up for a free account.

Similar topics

12
1993
by: Daniela Nii | last post by:
Hi there, I am looking for a javascript function that someone might have already written to calculate the paid-time-off. Given are the start and end date and time. I need to calculate the...
5
6714
by: SimonC | last post by:
Help needed for a Javascript beginner. As above in the subject... i need a javascript to run this, but not in the form of a web-page. I want to calculate it between 2 fields in a database that...
1
3323
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
18
5904
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. ...
7
25968
by: Sam | last post by:
Hi, I use C# in my ASP.NET projects. Here's what I need to do: I want to add x business days to a given date i.e. add 12 business days to today's date. What is the best, fastest and most...
3
5944
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
2
5015
by: rmmahara | last post by:
Hi Folks, I've been reading these forums for a while and now I'm in desperate need of help, so I thought I'd post! Background: I'm creating a Service Dashboard to track my team's adherence...
2
8702
by: rahulae | last post by:
help me with this I'm able to calculate total working days excluding weekends but how to exclude holidays,is there any other way apart from storing all the holidays in some table and not selecting...
4
3039
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.