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
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 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


Mark-443607
Mark-443607
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 83
thank you very helpful!!!
Dave Ballantyne
Dave Ballantyne
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2874 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