SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Exclude Weekend and Holiday
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

junior6202
Starting Member

USA
5 Posts

Posted - 07/07/2014 :  02:09:06  Show Profile  Reply with Quote
I wrote a query from a shipping table which subtracts two columns arrival date and shipping date. I used the DATEDIFF function and it work perfectly, but now I have to find a way to exclude the weekends and holidays from the results. For example, if a package arrived on Friday and was shipped on Monday it still counts as one business day excluding the weekend and the same goes for the holidays. I am not really sure how to achieve that, so any guidance will be really appreciated.

BBarn
Starting Member

USA
10 Posts

Posted - 07/07/2014 :  08:33:17  Show Profile  Reply with Quote
I've done this in the past for the exact same reason. The below logic should get you pointed in the right direction.

dw returns a number for the day of week represented by MyDate. Sunday is 1 Saturday is 7.


...
CASE DATEPART(dw, MyDate)
WHEN 1 then MyDate - 2 --sets it to Friday
WHEN 7 then MyDate - 1 --sets it to Friday
else MyDate
END as MyCalcDate
...

Edited by - BBarn on 07/07/2014 08:33:44
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
335 Posts

Posted - 07/08/2014 :  17:07:30  Show Profile  Reply with Quote
I suggest creating a NonWorkDays table and load it with all weekend, holiday and other non-work days (company shutdown, natural disaster, etc.). You then subtract the count of entries in the NonWorkDays calendar from the total days.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000