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

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Business Days

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-01-15 : 07:48:16
Lynn writes "I need to know the number of days between two dates. The twist is, I only wnat the number of Business Days (Meaning Monday through Friday. So, the difference between 1/1/2004 and 1/6/2004 should be 3 and not 5. 5 is what you get if you use DATADIFF(dd, '1/1/2004', '1/6/2004').

SQL Server 7.0 SP4 and will be going to SQL Server 2000 in 2 months.

Thanks,"

SamC
White Water Yakist

3467 Posts

Posted - 2004-01-15 : 08:25:06
I did a search on the forums and came up with this

[url]http://sqlteam.com/Forums/topic.asp?TOPIC_ID=10559&SearchTerms=business,days[/url]
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-01-15 : 09:00:34
This issue gets complicated also when you have to deal (count/discount/ignore) with bank/public holidays.....

Do a search for an item I posted about 2 years ago.....it explains more.
Go to Top of Page

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2004-01-15 : 13:26:46
A simple problem, really. You need a table containing all dates you wish to exclude, likely all weekends and holidays. Compute the straight DATEDIFF() and then subtract the # of rows from your exclusion table where the date falls in your date range. There's no "one size fits all" solution b/c everyone's definition of holiday is different (government vs. corporate vs. religious, etc).

Jonathan
Gaming will never be the same
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-01-15 : 14:22:51
quote:
Originally posted by setbasedisthetruepath

A simple problem, really. You need a table containing all dates you wish to exclude, likely all weekends and holidays. Compute the straight DATEDIFF() and then subtract the # of rows from your exclusion table where the date falls in your date range. There's no "one size fits all" solution b/c everyone's definition of holiday is different (government vs. corporate vs. religious, etc).

Jonathan
Gaming will never be the same



That's what I get for going to lunch...


CREATE TABLE WeekEndsAndHolidays (DayOfWeekDate datetime, DayName char(3))
GO

SET NOCOUNT ON
DECLARE @FirstSat datetime, @x int
SELECT @FirstSat = '1/3/2004', @x = 1

--Add WeekEnds
WHILE @x < 52
BEGIN
INSERT INTO WeekEndsAndHolidays(DayOfWeekDate, DayName)
SELECT DATEADD(ww,@x,@FirstSat), 'SAT' UNION ALL
SELECT DATEADD(ww,@x,@FirstSat+1), 'SUN'
SELECT @x = @x + 1
END
SET NOCOUNT OFF
GO

SELECT * FROM WeekEndsAndHolidays Order by DayOfWeekDate
GO

-- Add US Holidaze
INSERT INTO WeekEndsAndHolidays(DayOfWeekDate, DayName)
SELECT '1/1/2004', 'THU' UNION ALL
SELECT '2/16/2004', 'MON' UNION ALL
SELECT '5/31/2004', 'MON' UNION ALL
SELECT '7/5/2004', 'MON' UNION ALL
SELECT '9/6/2004', 'MON' UNION ALL
SELECT '11/25/2004', 'THU' UNION ALL
SELECT '11/26/2004', 'FRI' UNION ALL
SELECT '12/24/2004', 'FRI' UNION ALL
SELECT '12/31/2004', 'FRI'
GO

SELECT * FROM WeekEndsAndHolidays Order by DayOfWeekDate
GO


-- WeekDays in May

DECLARE @Start datetime, @End datetime
SELECT @Start = '5/1/2004', @End = '6/1/2004'

SELECT DATEDIFF(dd,@Start,@End)- COUNT(*) AS WeekDays
FROM WeekEndsAndHolidays
WHERE DayOfWeekDate BETWEEN @Start AND @End
GO





Brett

8-)
Go to Top of Page
   

- Advertisement -