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.
| Author |
Topic |
|
mac_85
Starting Member
3 Posts |
Posted - 2009-11-15 : 18:36:51
|
| I want to create a function that allows me to add a selected number of days to a given date, and determine the end point.I need the function to exclude weekends, and known holidays.I've used the function from Michael Valentine Jones (MVJ) to create a table with this data in; this is named 'DbDatePart'.I have a table with Holiday Dates 'DbHolidays', columnsHolidaysId - intLocationId - intHolidayDate - datetimeHolidayName - varchar(50)Various regions have their own LocationId with their holiday dates.How would I create a function that allows me to have 'StartDate', 'DaysToAdd' to give me an 'EndDate' that doesn't count weekends (Saturday and Sunday) and dates from DbHolidays.HolidayDate when DbHolidays.LocationId = 'LocationId'.Any help would be greatly apprecaited!!! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-11-16 : 03:22:07
|
SELECT MAX(Date) FROM (SELECT TOP(50) f.DateFROM F_TABLE_DATE('20091116', '2099116') AS fWHERE WEEKDAY_NAME NOT IN ('Sat', 'Sun')AND f.Date NOT IN (SELECT x.HolidayDate FROM DbHolidays AS x)ORDER BY f.Date ) AS d N 56°04'39.26"E 12°55'05.63" |
 |
|
|
mac_85
Starting Member
3 Posts |
Posted - 2009-11-16 : 16:43:20
|
quote: Originally posted by Peso SELECT MAX(Date) FROM (SELECT TOP(50) f.DateFROM F_TABLE_DATE('20091116', '2099116') AS fWHERE WEEKDAY_NAME NOT IN ('Sat', 'Sun')AND f.Date NOT IN (SELECT x.HolidayDate FROM DbHolidays AS x)ORDER BY f.Date ) AS d N 56°04'39.26"E 12°55'05.63"
Thanks - I get the error message"Conversion failed when converting datetime from character string."It looks like this will return a date that's not a Saturday or Sunday, or in the Holiday date within the range given for the function.What I want to be able to do is pass the parameters StartDate, DaysToAdd, LocationId so that:StartDate = 2009-Nov-17DaysToAdd = 12LocationId = 2to Give me an EndDate of 2009-Dec-03 that I can use in a Stored Procedure AS dbo.FindDueDate(DaysToAdd, StartDate, LocationId).I've found this UDF[dbo].[AddWorkDays] ( @WorkingDays As Int, @StartDate AS DateTime ) RETURNS DateTime AS BEGIN DECLARE @Count AS Int DECLARE @i As Int DECLARE @NewDate As DateTime SET @Count = 0 SET @i = 0 WHILE (@i < @WorkingDays) --runs through the number of days to add BEGIN -- increments the count variable SELECT @Count = @Count + 1 -- increments the i variable SELECT @i = @i + 1 -- adds the count on to the StartDate and checks if this new date is a Saturday or Sunday -- if it is a Saturday or Sunday it enters the nested while loop and increments the count variable WHILE DATEPART(weekday,DATEADD(d, @Count, @StartDate)) IN (1,7) BEGIN SELECT @Count = @Count + 1 END END -- adds the eventual count on to the Start Date and returns the new date SELECT @NewDate = DATEADD(d,@Count,@StartDate) RETURN @NewDate END Can I modify this to consider dates in DbHolidays to ignore where LocationId is the same as that passed in?So if StartDate = 2009-Nov-17DaysToAdd = 12LocationId = 2And LocationId 2 has a date of 2009-Dec-02 it will return an EndDate of 2009-Dec-04. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-11-17 : 03:21:36
|
quote: Originally posted by mac_85 SELECT MAX(Date) FROM (SELECT TOP(50) f.DateFROM F_TABLE_DATE('20091116', '20991116') AS fWHERE WEEKDAY_NAME NOT IN ('Sat', 'Sun')AND f.Date NOT IN (SELECT x.HolidayDate FROM DbHolidays AS x)ORDER BY f.Date ) AS d
N 56°04'39.26"E 12°55'05.63" |
 |
|
|
mac_85
Starting Member
3 Posts |
Posted - 2009-11-17 : 15:25:03
|
| Thanks, the code now runs, although its fairly heavy.But I don't see that it gives me what I'm after. Running this gives me Jan-22-2010, whereas what I really want to do is in my previous post:StartDate = 2009-Nov-17DaysToAdd = 12LocationId = 2 (and no values in DbHolidays)EndDate returned = 2009-Dec-03 ANDStartDate = 2009-Nov-17DaysToAdd = 12LocationId = 2 (LocationId 2 has a date of 2009-Dec-02 in DbHolidays)EndDate returned = 2009-Dec-04. |
 |
|
|
|
|
|
|
|