Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

Starting Member

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

Starting Member

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

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

Aged Yak Warrior

550 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  
 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.02 seconds. Powered By: Snitz Forums 2000