Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Count Business Days Expand / Collapse
Author
Message
Posted Friday, July 11, 2008 9:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 22, 2008 9:18 AM
Points: 4, Visits: 13
Comments posted to this topic are about the item Count Business Days
Post #532609
Posted Friday, July 18, 2008 8:14 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:24 PM
Points: 3,997, Visits: 6,049
If you prepopulate a "calendar" table with your weekend days and holidays, you can also do a simple query as shown in the following example. Please note that the great majority of the code which follows is actually the code used to populate a table variable which on our system is a permanent table . All the work of calculating business days is done in the last three lines. (The tally table is simply a table of numbers [N] from one to a million which I always create in any db. )

Having a calendar table also lets you do some cute things such as having a column that designates whether the holiday is for U.S., Canada, or both, which lets your function take a parameter to pass to your where clause to pick the appropriate holidays. You might also want an option to NOT include Saturdays as business days, or to add 1 to the datediff. Different clients count turn times in different ways.

-----------------------------------------------------------------------------------------------------------------
-- example
-----------------------------------------------------------------------------------------------------------------


declare @start datetime
declare @end datetime
declare @weekends_holidays table (offday datetime primary key, weekday int)

select @start = '7/1/2008',@end = '7/10/2008'


-- the following query populates the table variable with some weekend days and the 4th of July
insert into @weekends_holidays
select distinct @start+N-1,datepart(dw,@start+N-1)
from tally
where N <= datediff(dd,@start,@end)
and datepart(dw,@start+N-1) in (1,7) -- living for the weekend
union all
select '7/4/2008',datepart(dw,'7/4/2008') -- three cheers for the red, white, and blue


select * from @weekends_holidays

-------------------------------------------------------------------------
-- all of the above was just to set up the following query
-- in practice, @weekends_holidays would be a permanent table
-------------------------------------------------------------------------

select datediff(dd,@start,@end) - count(*) as businessDays
from @weekends_holidays
where offday between @start and @end




__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #536827
Posted Friday, July 18, 2008 10:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:25 PM
Points: 5,563, Visits: 24,684
Created the function using copy / paste / execute - no reported error then tested using the following:

DECLARE @Bdays AS INT
SET @Bdays = dbo.fn_GetBusinessDays('07/06/2008','07/27/2008')
SELECT @Bdays
Result returned 14 ..
Now counting on my fingers it is actually 15 days


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #536950
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse