Solved

SQL - Date difference excluding weekends

Posted on 2013-06-21
5
764 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 40

Accepted Solution

by:
Kyle Abrahams earned 250 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 73

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 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
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.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now