Add ‘n’ working days to a given date

>>Script Language and Platform: SQL 7.0/2000
This script includes stored procedure and function.

The user only needs to specify the given date (datetime) and how many workdays after the given date (int).

The result will skip weekends and give you the correct workday result,even if your starting date is specified as Saturday or Sunday.

The stored procedure can be used in either SQL7/2000; the function can be only created in SQL2000.

Author: Claire Hsu


/*
* Script Language and Platform:MS SQL 7/2000
* Objecttive: Add ‘n’ working days to the given date
* Usage:Which date it is?Add 16 workdays to ‘1/1/2003’?
* select dbo.getwork(‘1/1/2003′,’16’)
* Author:Claire Hsu
* Date :2003/7/17
* Email:[email protected]
* Description:This script contains both funciton and procedure.
* You could apply accordingly
*/

–Here is the code for Function
——————————————————————————-
Create function dbo.getwork (@date datetime,@nd int)
returns datetime
as
begin

declare @wk int
select @wk = datepart(dw,@date)
declare @work datetime

if @nd > ([email protected])
begin
if (@nd-([email protected]))%5 = 0
set @work = @date+([email protected])+7*((@nd-([email protected]))/5)+(@nd-([email protected]))%5
else
set @work = @date+([email protected])+7*((@nd-([email protected]))/5)+(@nd-([email protected]))%5+2
end
if @nd <= ([email protected]) begin set @work = @[email protected] end if @wk = 7 begin if @nd%5 = 0 set @work = @date+7*((@nd)/5)-1 if @nd%5<>0
set @work = @date+7*((@nd)/5)[email protected]%5
end

if @wk = 1
begin
if @nd%5 = 0
set @work = @date+7*((@nd)/5)-2
if @nd%5<>0
set @work = @date+7*((@nd)/5)[email protected]%5
end
return (@work)
end

–Usage
–select dbo.getwork(‘1/1/2003′,’16’)
–SELECT dbo.getwork(col_name, 9) from tablename

—————————————————————————-
—————————————————————————-

–Here is the code for Stored Procedure
Create proc getwork @date datetime,@nd int
as
declare @wk int
select @wk = datepart(dw,@date)
declare @work datetime

if @nd > ([email protected])
begin
if (@nd-([email protected]))%5 = 0
set @work = @date+([email protected])+7*((@nd-([email protected]))/5)+(@nd-([email protected]))%5
else
set @work = @date+([email protected])+7*((@nd-([email protected]))/5)+(@nd-([email protected]))%5+2
end
if @nd <= ([email protected]) begin set @work = @[email protected] end if @wk = 7 begin if @nd%5 = 0 set @work = @date+7*((@nd)/5)-1 if @nd%5<>0
set @work = @date+7*((@nd)/5)[email protected]%5
end

if @wk = 1
begin
if @nd%5 = 0
set @work = @date+7*((@nd)/5)-2
if @nd%5<>0
set @work = @date+7*((@nd)/5)[email protected]%5
end
select @work

–Usage
–exec getwork ‘1/2/2003′,’17’



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Latest Articles