473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to get business days between two dates?

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 31920
amitpatel66
2,367 Expert 2GB
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
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
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 Expert 4TB
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
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...
12
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...
1
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...
4
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...
7
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...
2
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
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
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
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...
7
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.