Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Calculating time differences, excluding weekends
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Calculating time differences, excluding weekends

Brian Peasland EXPERT RESPONSE FROM: Brian Peasland

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 26 April 2002

I have a table containing following Columns plus other columns:

 Start Date 04-01-2002(mm-dd-yyyy)  		varchar(10)
 Start Time column 09:30:30(hh:mm:ss)  	 varchar(8)
 End Date 04-05-2002(mm-dd-yyyy)        	varchar(10)
 End Time column 09:45:30(hh:mm:ss)   	varchar(8)
1. We want to create a view for report users, so that they could just select the other columns and difference in start Date/time to End Date/Time.
2. The difference in time should be based on business day. i.e. Saturday & Sunday should be excluded.
3. Each business day is defined as 8:00 AM to 5:00 PM (1 business day = 9 hrs)
4. The time is stored as a 24-hour clock.

For example:

Start Date   Start Time		End Date       End Time      Desired Output	
04/01/2002   10:00:00		04/01/2002     3:30:00	       0 Days  5:30 Hrs
04/01/2002   16:00:00		04/02/2002     10:45:00	       0 Days  3:45 Hrs
04/01/2002   09:00:00		04/03/2002     10:45:00	       2 Days  1:45 Hrs
04/05/2002   16:30:00		04/08/2002     9:45:00	       0 Days  2:15 Hrs*

*In last case as 04/06/2002 & 04/07/2002 were Saturday & Sunday, hence 
excluded. So the difference of time is just 2:15 hrs.
Can you please give me a suggestion/pseudo code/logic on how I might do this? We cannot use cursors as we want to make a view.



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


This type of arithmetic isn't going to work very easy since you are not using Oracle's native DATE datatype. If you used one column of the DATE datatype to denote the starting time and date, and another column to denote the ending date and time, then you could just subtract the two values and it would tell you the difference! The DATE datatype makes this really easy.

The next trick is to then figure out how many weekends are in that date range and subtract 2 days for the weekend. The DECODE function can be used in your query to perform if/then analysis and return different results.

For More Information

  • What do you think about this answer? E-mail the editors at editor@searchDatabase.com with your feedback.
  • The Best Oracle Web Links: tips, tutorials, scripts, and more.
  • Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.




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