473,549 Members | 2,592 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 9211

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

Similar topics

12
1997
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 paid-time-off. That means the formula needs to be smart enough to figure out the Saturdays and Sundays within the period and subtract the time from the...
5
6719
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 i have extracted into a report writer. Look forward to hearing.. Cheers... SimonC
1
3329
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 Function explanation Part II
18
5908
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. More holidays can be added easily. Also, there is a boolean flag for including or excluding Saturdays or Sundays as part of the work week. The only...
7
25975
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 efficient way for me to do this? -- Thanks, Sam
3
5951
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 calculate in the query the amount of weeks and days within the 17 days to show 2 weeks 3 days. I can create the function within excel, (A1 as cell...
2
5027
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 to Service Levels. As part of this, I've set up a web page which pulls data from a query in Access 2003. The page shows which tickets we're working on,...
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 those or else is there any other option
4
3046
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 the function using the intermediate window, it works fine. However, when I pass the dates from the code attached to my form, the results are inaccurate....
0
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5097
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
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 we have to send another system
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.