Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding workdays


Adding workdays

Author
Message
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Sergiy, this goes with my last trigger post, but this is kind of a new topic. I made a test table for emails with a column for delivery_dt as you suggested. I thought all was well, but then I realized I did not know how to add say 5 workdays. Using Jeffs marvelous code for calculating the number of workdays between two dates helped me know how many work days had passed given a startdate, but now I need to be able to set the delivery_dt = startdate plus 5 workdays. I thought it would be easy, but I am completely stumped. I started by making a UDF like so...

IF OBJECT_ID('dbo.fnAddWorkdays','fn') IS NOT NULL
DROP FUNCTION dbo.fnAddWorkdays
GO
CREATE FUNCTION dbo.fnAddWorkdays (@Startdate DATETIME,@NumberDays INT)
RETURNS DATETIME
AS
BEGIN
DECLARE @EndDate DATETIME
SELECT @EndDate = DATEADD(d,@NumberDays,@Startdate)
SELECT @EndDate = DATEADD(d,
CASE WHEN DATENAME(dw,@Enddate) = 'Saturday' THEN 2
WHEN DATENAME(dw,@Enddate) = 'Sunday' THEN 1
ELSE 0
END,@EndDate)
RETURN @Enddate
END

SELECT dbo.fnAddWorkdays(GETDATE(),5)

I quickly realized, though, that it was not going to be that easy. My function only adds 1 or 2 if the enddate is saturday or sunday. It does not account for the weekend days in between. So, anyone have any ideas?

Greg

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Daryl AZ
Daryl AZ
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 683
This probably isn't the most efficient way but...


Create a date table (reasonable range)
weekdays (id, dayDate)
with only weekday dates.

FROM invc
inner join weekdays currDate on invc.mydate = currDate.dayDate --may need both at midnight
inner join weekdays datePlus5 on currDate.id + 5 = datePlus5.id

The first inner join may need a caveat if invoices can come in on weekends....

daryl
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Thanks Daryl. I want to do it without having to make a days table. I know there is a way, I just can't get my head around it

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Ok, I figured it out. I knew it had to be a reverse of Jeff's code, which can be found at

www.sqlservercentral.com/articles/Advanced+Querying/calculatingworkdays/1660/.

And it was. I had to read the article several times to really understand what was happening, but basically what I needed to do is this

IF OBJECT_ID('dbo.fnAddWorkdays','fn') IS NOT NULL
DROP FUNCTION dbo.fnAddWorkdays
GO
CREATE FUNCTION dbo.fnAddWorkdays (@Start DATETIME,@NumDays INT)
RETURNS DATETIME
AS
BEGIN
DECLARE @EndDate DATETIME
SELECT @EndDate =
DATEADD(d,@NumDays,@Start) + (DATEDIFF(wk,@Start,DATEADD(d,@NumDays,@Start))*2)
+(CASE WHEN DATENAME(dw,@Start) = 'Sunday' THEN 1 ELSE 0 END)
+(CASE WHEN DATENAME(dw,@Start) = 'Saturday' THEN 1 ELSE 0 END)
RETURN @Enddate
END

And I can test it against Jeff's code

SELECT dbo.fnAddWorkdays('1/1/2007',20) returns 1/27/2007. If I use 1/27/2007 as the end date I can run Jeff's code to get 20

DECLARE
@Start DATETIME,
@End DATETIME
SELECT @Start = '1/1/2007'
SELECT @End = '1/27/2007'
SELECT
(DATEDIFF(dd, @Start, @End) + 1)-(DATEDIFF(wk, @Start, @End) * 2)
-(CASE WHEN DATENAME(dw, @Start) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @End) = 'Saturday' THEN 1 ELSE 0 END)

Thanks again Jeff. Just another example of how you continue to teach me without even knowing it.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Well...it seems to have some problems. If I start on a week day I should always end on a week day, but my function does not seem to do that. See for yourself. I guess its back to the drawing board.

IF OBJECT_ID('TempDB..#Test','u') IS NOT NULL
DROP TABLE #Test
CREATE TABLE #Test
(
Start SMALLDATETIME
)
INSERT INTO #Test
SELECT '1/1/2007' UNION ALL
SELECT '1/2/2007' UNION ALL
SELECT '1/3/2007' UNION ALL
SELECT '1/4/2007' UNION ALL
SELECT '1/5/2007' UNION ALL
SELECT '1/6/2007' UNION ALL
SELECT '1/7/2007' UNION ALL
SELECT '1/8/2007' UNION ALL
SELECT '1/9/2007' UNION ALL
SELECT '1/10/2007' UNION ALL
SELECT '1/11/2007' UNION ALL
SELECT '1/12/2007' UNION ALL
SELECT '1/13/2007' UNION ALL
SELECT '1/14/2007' UNION ALL
SELECT '1/15/2007' UNION ALL
SELECT '1/16/2007' UNION ALL
SELECT '1/17/2007' UNION ALL
SELECT '1/18/2007' UNION ALL
SELECT '1/19/2007' UNION ALL
SELECT '1/20/2007' UNION ALL
SELECT '1/21/2007' UNION ALL
SELECT '1/22/2007' UNION ALL
SELECT '1/23/2007' UNION ALL
SELECT '1/24/2007' UNION ALL
SELECT '1/25/2007' UNION ALL
SELECT '1/26/2007' UNION ALL
SELECT '1/27/2007' UNION ALL
SELECT '1/28/2007' UNION ALL
SELECT '1/29/2007' UNION ALL
SELECT '1/30/2007' UNION ALL
SELECT '1/31/2007'

SELECT
Start,
DayOfWeek1 = DATENAME(dw,start),
StartAdd5 = dbo.fnAddWorkdays(start,5),
DayOfWeek2 = DATENAME(dw,dbo.fnAddWorkdays(start,5))
FROM #Test

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Ok, I think I fixed it. I forgot one thing from Jeffs function, then I just fiddled with the numbers until it seemed to work.

IF OBJECT_ID('dbo.fnAddWorkdays','fn') IS NOT NULL
DROP FUNCTION dbo.fnAddWorkdays
GO
CREATE FUNCTION dbo.fnAddWorkdays (@Start DATETIME,@NumDays INT)
RETURNS DATETIME
AS
BEGIN
DECLARE @EndDate DATETIME
SELECT @EndDate =
(DATEADD(d,@NumDays,@Start)-1) + -- I forgot the -1 here (DATEDIFF(wk,@Start,DATEADD(d,@NumDays,@Start))*2)
+(CASE WHEN DATENAME(dw,@Start) = 'Sunday' THEN 1 ELSE 0 END)
+(CASE WHEN DATENAME(dw,@Start) = 'Saturday' THEN 3 ELSE 0 END) --changed the 1 to a 3 and it works

RETURN @Enddate
END

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
False alarm. There is still something wrong with saturdays.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Kenneth Wilhelmsson
Kenneth Wilhelmsson
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1579 Visits: 699
So, what's the deal with not having a dates table?
It will solve all your problems, no need for complex functions or anything else exotic.

The main problem: How do you define 'workday' programatically?
You can't. Simple as that.

'Workday' is a business rule, it differs from place to place, so you need to single out those 'special' days anyway. Might as well do it once, store it in a permanent table for lookups, then it's always there to be found. No need to try to calculate it everytime.
Do it once, then forget about it. Smile

/Kenneth



Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Kenneth, I probably should refer to it as weekdays, but as far as my production is concerned no weekends are work days. I may end up having to make a table, but I am sure what I am trying to do is possible. If Jeff's code can take a start and end date and count all the week days, that is like saying b - a = X, where a and b are both known, and a is @Start and b is @End and X is an unknown integer. Logically, all I want to do is shift it around to be a + X = b where a is @Start and X is a known integer. Do you see any reason why this would not be so? I am just not skilled enough to do it, but I will keep working on it.

Greg

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Matt Miller (#4)
Matt Miller (#4)
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7641 Visits: 18066
Try this one on:
--set up
declare @dateStart datetime
declare @daysToAdd int
declare @dateEnd datetime
declare @baseday datetime

select @datestart='9/1/2007',@daysToAdd=52
--start by making sure your start day IS a business day
Select @datestart=dateadd(dd,case when cast(cast(@datestart as datetime) as int)%7 >4 then
7-cast(cast(@datestart as datetime) as int)%7 else 0 end,@datestart)
select @datestart
--the magic
select @dateEnd=dateadd(dd,
case when cast(cast(dateadd(dd,@daystoAdd%5,@datestart) as datetime) as int)%7 >4 then
7-cast(cast(dateadd(dd,@daystoAdd%5,@datestart) as datetime) as int)%7 else 0 end,
dateadd(dd,@daystoAdd%5,dateadd(wk,@daysToAdd/5,@datestart)))
select @dateend

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
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