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.

• 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.

#### Have a question for an expert?

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.

## SearchDataManagement

• ### MariaDB X4 brings smart transactions to open source database

Open source database vendor MariaDB updates its flagship platform with new features to enable the convergence of transactional ...

• ### How data lineage tools boost data governance policies

Organizations can bolster data governance efforts by tracking the lineage of data in their systems. Get advice on how to do so ...

• ### Proactive practices for data quality improvement

Instead of waiting for data quality to become an issue, consider a proactive approach. Here are some ways to improve data quality...

• ### Storytelling using data makes information easy to digest

In a Q&A, Nate Nichols and Anna Schena Walsh of AI-based analytics vendor Narrative Science talk about how data storytelling can ...

• ### Data-driven storytelling makes data accessible

In a Q&A, Nate Nichols and Anna Schena Walsh of data storytelling vendor Narrative Science discuss their book on storytelling and...

• ### Startup Uplevel targets software engineering efficiency

Startup vendor Uplevel emerged from stealth on Wednesday touting a platform aimed at improving the efficiency of software ...

## SearchSAP

• ### SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2025, organizations must make some tough decisions. Here's a look at your choices.

• ### New SAP leadership faces big challenges in 2020

Industry analysts discuss SAP's biggest issues in 2020, including how the two new CEOs will guide the company deeper into the ...

• ### SAP Data Hub opens predictive possibilities at Paul Hartmann

When medical supply firm Paul Hartmann AG tested a supply chain analysis system built on SAP Data Hub, it found that it could ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Don't ever put a non-Java LTS release into production

Development teams should avoid non-long-term support releases at all costs. Pay attention to the Java release cycle to make sure ...

• ### Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• ### Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

## SearchDataCenter

• ### Organizations try to predict the effect of 5G infrastructure

With more 5G and IoT devices emerging, admins must prepare their data centers to support low-latency apps and edge computing with...

• ### Top infrastructure and operations technology myths of 2019

Admins are consistently evaluating technology to improve I&O efficiency. Cost, integration and business goals are key components ...

• ### Improve efficiency with server energy consumption tools

Deciding what servers are most efficient for your infrastructure requires research. Hardware-level certifications and web-based ...

## SearchContentManagement

• ### Ex-SAP exec steers Episerver CMS toward digital experience market

Alex Atzberger discusses leaving the helm of SAP's CX platform to become Episerver CEO. Now, Episerver looks to reinvent itself ...

• ### 4 digital transformation terms to know

As technology evolves, businesses have no choice but to follow. Digital transformations are inevitable in order to keep customers...

• ### How to differentiate between CX and DX platforms

CX and DX platforms have similarities in how they manage content to produce interactive experiences, but they aren't the same. ...

## SearchHRSoftware

• ### Top 7 HR trends for 2020 and beyond: The change decade

Because HR requires a balance between the needs of the organization with the needs of employees, it's always been a balancing act...

• ### For insider threat programs, HR should provide checks and balances

Insider threat programs may backfire if employees feel they are intrusive and violate privacy, Forrester Research warns. Making ...

• ### Better HR security could help thwart Iranian cyberattack

HR departments have a broad role to help protect their firms from Iranian cyberattacks. The security efforts include helping with...

Close