Solved

SQL - Date difference excluding weekends

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New and Previous Values in a Query 7 30
SQl Query to find x consecutive Nbrs in a Table 30 95
SQL Use Distinct with two fields 3 14
SQL - Simple Pivot query 8 11
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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