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


DateAdd function for Weekdays


DateAdd function for Weekdays

Author
Message
Jesse Roberts
Jesse Roberts
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 1

Here's a function for doing dateadd for weekdays only. (e.g., what weekday is 43 weekdays from today?) Hope it is helpful.

CREATE FUNCTION dbo.weekday_dateadd ( @nbrOfDays int, @startDate smalldatetime )

RETURNS smalldatetime
AS
BEGIN

/*
Title: weekday_dateadd
Author: Jesse Roberts
Date: 08/03/2005
Purpose: Adds a specified number of weekdays to a given date and returns the resulting date
*/


declare @adj_start smalldatetime, @endDate smalldatetime
declare @adj_nbrOfDays real, @adj_dayofweek int
declare @numweeks int, @adj_enddays int, @calendar_days int

-- get to the last Monday prior to the start date
set @adj_dayofweek = (datepart(dw,@startDate) - 2)
set @adj_start = dateadd(d, -(@adj_dayofweek), @startDate)

-- round to the nearest multiple of 5
set @adj_nbrOfDays = round(@nbrOfDays / 5, 0) * 5

-- adjust the rounding product to ensure the result is the last multiple of 5 less than @increment
if @adj_nbrOfDays > @nbrOfDays
begin
set @adj_nbrOfDays = @adj_nbrOfDays - 5
end

-- add up the number of calendar days to add back to the end date
set @adj_enddays = (@nbrOfDays - @adj_nbrOfDays) + @adj_dayofweek

if @adj_enddays > 4 -- if @adj_enddays is > 4, then it means Friday of the week was passed since 2 (Mon) + 4 = 6 (Fri)
begin
set @adj_enddays = @adj_enddays + 2 -- add 2 days to compensate for the Sat/Sun wrap
end

set @calendar_days = (@adj_nbrOfDays * 1.4) + @adj_enddays

set @endDate = dateadd(d, @calendar_days, @adj_start)

set @endDate = case datepart(dw,@endDate)
when 7 then dateadd(d,2,@endDate)
when 1 then dateadd(d,2,@endDate)
else @endDate
end

RETURN @endDate
END


Site Owners
Site Owners
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: Administrators
Points: 12669 Visits: 17
No one has responded to this topic yet. Even if you don't have a complete answer, the original poster will appreciate any thoughts you have!
Mark-443607
Mark-443607
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 83
thank you very helpful!!!
Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7140 Visits: 8370
Since this has been bumped ,

a calendar table might be a good option here

http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html



Clear Sky SQL
My Blog
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