SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count Business Days


Count Business Days

Author
Message
megan.beebe
megan.beebe
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 13
Comments posted to this topic are about the item Count Business Days
The Dixie Flatline
The Dixie Flatline
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21933 Visits: 6911
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? Everybody look what's going down. -- Stephen Stills
bitbucket-25253
bitbucket-25253
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26869 Visits: 25280
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search