Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL - Date difference excluding weekends

Posted on 2013-06-21
5
Medium Priority
?
774 Views
Last Modified: 2013-06-24
Hello experts,

I have a need to calculate the number of business days between 2 dates and need to exclude business days.

I found this example, but it's not recognizing "TO_CHAR".

select sysdate - creation_dttm
- 2 * (to_char(sysdate, 'WW') - to_char(creation_dttm, 'WW'))
from the_table

Any ideas?
0
Comment
Question by:holemania
5 Comments
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 1000 total points
ID: 39267212
From http://msdn.microsoft.com/en-us/library/ms189794(v=sql.90).aspx:

CREATE FUNCTION "fnGetBusinessDays"
(
@startdate datetime,
@enddate datetime
)
RETURNS integer
AS
BEGIN
DECLARE @days integer

SELECT @days = 
DATEDIFF(d,@startdate,@enddate) 
- DATEDIFF(wk,@startdate,@enddate) * 2 
- CASE 
WHEN DATENAME(dw, @startdate) <> 'Saturday' AND DATENAME(dw, @enddate) = 'Saturday' THEN 1 
WHEN DATENAME(dw, @startdate) = 'Saturday' AND DATENAME(dw, @enddate) <> 'Saturday' THEN -1 
ELSE 0
END
- (SELECT COUNT(*) FROM holidays WHERE bankholiday BETWEEN @startdate AND @enddate AND DATENAME(dw, bankholiday) <> 'Saturday' AND DATENAME(dw, bankholiday) <> 'Sunday')

RETURN (@days)

END
GO

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39267227
your tags say sql2005, but your sample syntax is for Oracle

so, here's an oracle sql method

  SELECT
           (TRUNC(SYSDATE) - TRUNC(creation_dttm))
         - NVL(
               (    SELECT COUNT(
                               CASE
                                   WHEN TO_CHAR(creation_dttm + LEVEL - 1, 'Dy') IN ('Sat', 'Sun')
                                   THEN  1                                
                               END
                           )
                      FROM DUAL
                CONNECT BY LEVEL < (TRUNC(SYSDATE) - TRUNC(creation_dttm))),
               0
           ) diff            
    FROM the_table
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 1000 total points
ID: 39267438
yep, it's confusing when the topics/tags say one thing but the query says another

so if SQL Server: here's a TSQL method without needing a function
SELECT
  ID
,  (DATEDIFF(dd, creation_dttm, getdate()) + 1)
  -(DATEDIFF(wk, creation_dttm, getdate()) * 2)
  -(CASE WHEN DATENAME(dw, creation_dttm) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, getdate()) = 'Saturday' THEN 1 ELSE 0 END)
  as working_days
, <<other fierlds >>
from your_table

Open in new window

0
 

Author Comment

by:holemania
ID: 39272910
Sorry I saw an example and posted here.  Didn't know it was oracle, but yes I'm looking for SQL.  Thank you.
0
 

Author Closing Comment

by:holemania
ID: 39272911
Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question