473,529 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get business days between two dates?

40 New Member
Hi!
There are two given dates and I have to find out the working days between two dates i.e. excluding Sundays. Please help. Thank you.
Feb 6 '08 #1
5 31953
amitpatel66
2,367 Recognized Expert Top Contributor
Hi!
There are two given dates and I have to find out the working days between two dates i.e. excluding Sundays. Please help. Thank you.
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT dt FROM
  3. (SELECT TO_DATE(:start_date,'DD-MON-YYYY') + level - 1 dt FROM table_name CONNECT BY level <= TO_DATE(:end_date,'DD-MON-YYYY') - TO_DATE(:start_date,'DD-MON-YYYY')) WHERE TO_DATE(dt,'Day') != 'Sunday';
  4.  
  5.  
PS: Not Tested!!
Feb 6 '08 #2
subashsavji
93 New Member
Hi!
There are two given dates and I have to find out the working days between two dates i.e. excluding Sundays. Please help. Thank you.

MODIFIED OF AMITH PETELS CODE
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT HIREDATE FROM 
  3. (SELECT TO_DATE(&start_date,'DD-MON-YYYY') + level - 1 HIREDATE FROM EMP
  4.  CONNECT BY level <= TO_DATE(&end_date,'DD-MON-YYYY') - TO_DATE(&start_date,'DD-MON-YYYY') ) 
  5. WHERE TO_DATE(HIREDATE,'Day') != 'Sunday'
  6. /
  7.  
Feb 7 '08 #3
subashsavji
93 New Member
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT dt FROM
  3. (SELECT TO_DATE(:start_date,'DD-MON-YYYY) + level - 1 dt FROM table_name CONNECT BY level <= TO_DATE(:end_date,'DD-MON-YYYY) - TO_DATE(:start_date,'DD-MON-YYYY')) WHERE TO_DATE(dt,'Day') != 'Sunday';
  4.  
  5.  
PS: Not Tested!!
Expand|Select|Wrap|Line Numbers
  1.  
  2. WITH date_tab AS
  3.          (SELECT       TO_DATE ('&from_date', 'dd-MON-yyyy')
  4.                      + LEVEL
  5.                      - 1 business_date
  6.                 FROM DUAL
  7.           CONNECT BY LEVEL <=
  8.                           TO_DATE ('&to_date', 'dd-MON-yyyy')
  9.                         - TO_DATE ('&from_date', 'dd-MON-yyyy')
  10.                         + 1)
  11.   SELECT business_date
  12.     FROM date_tab
  13.    WHERE TO_CHAR (business_date, 'DY') NOT IN ('SAT', 'SUN');
  14. //
  15.  

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE OR REPLACE FUNCTION F_BUSINESS_DAYS
  3. (V_START_DATE IN DATE, V_END_DATE IN DATE)
  4. RETURN NUMBER IS DAY_COUNT NUMBER := 0;
  5. CURR_DATE DATE;
  6. BEGIN -- loop through and update
  7. CURR_DATE := V_START_DATE;
  8. WHILE CURR_DATE <= V_END_DATE
  9. LOOP
  10. IF TO_CHAR(CURR_DATE,'DY') NOT IN ('SAT','SUN')
  11. THEN DAY_COUNT := DAY_COUNT + 1;
  12. END IF;
  13. CURR_DATE := CURR_DATE + 1;
  14. END LOOP;
  15. RETURN DAY_COUNT;
  16. END F_BUSINESS_DAYS;
  17. //
  18.  
  19.  
Feb 7 '08 #4
Dear Subhashsavji,

The solution (WITH date_tab AS ) works fine...great query.

Many Thanks.
Oct 15 '10 #5
debasisdas
8,127 Recognized Expert Expert
Also check this for more customized version.
Oct 18 '10 #6

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

Similar topics

1
4243
by: David Stockwell | last post by:
I''m wondering if the Calendar object has an option to only do calculations based on business days (ie M-F). Additionally does it have a way to get holidays into itself? Currently I'm considering converting my calculations over to business days and am wondering if there is anything built-in. I've tried searching but so far haven't seen...
12
23671
by: Anthony Robinson | last post by:
Is anyone aware of a function (system or user defined) that will calculate business days? For instance: I have a column in as table called DATE. I want to be able to add five business days to that date and come up with a new date. Is that possible. Also, is there anyway that DB2 can be aware of holidays? Maybe load them onto the server in...
1
3721
by: igendreau | last post by:
I have users inputting a "Request Date". Upon entering a date, I need Access to populate a second field ("Due Date"). When they enter their Request Date, I want Access to set the default value of Due Date = Request Date + 9 Business Days (Holidays don't matter. Just want Saturday and Sunday taken out). Any thoughts?
4
6148
by: CDMAPoster | last post by:
I'm starting to come up with a version of DateAdd that I call BusinessDateAdd that adds the selected number of business days. It's still in preliminary form (needs testing) and interacts with my direct date functions (which have also changed slightly). There's no room to put the code in the margins :-), but there's enough room to put up a...
7
25973
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
2
2757
by: brains4math | last post by:
Hi, is there a method in C# that calculates the number of business days between given dates, like networkdays in Excel? Thank you!
8
7483
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
1
7176
by: santhoshrms | last post by:
I need to be able to determine the number of business days between two dates. Is there a function/Select Query in DB2 that can be used to do this? ...
5
24526
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate business days count which is defined as count of days (optionally inclusive in the current implementation) excluding weekend days and holidays. Let us...
7
1760
Jerry Maiapu
by: Jerry Maiapu | last post by:
Ok, I found a function that calculates/count business days excluding Weekends and holidays on the net. My country's Holiday dates are in a table tblHolidays with fields. HolidayID,HolidayDate,HolidayName. The Function: Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer '-- Return the number of WorkingDays between...
0
7345
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7508
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7671
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
7258
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
5809
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...
1
5199
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3335
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...
0
1719
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
567
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.