Q
Problem solve Get help with specific problems with your technologies, process and projects.

Calculating time differences, excluding weekends

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.


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.

Dig Deeper on Oracle database design and architecture

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close