Add 'n' working days to a given date | Database Journal

Add ‘n’ working days to a given date

Jul 18, 2003
1 minute read

>>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:messageclaire@yahoo.com
   *	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 > (6-@wk)
	begin
	if (@nd-(6-@wk))%5 = 0
	set @work = @date+(6-@wk)+7*((@nd-(6-@wk))/5)+(@nd-(6-@wk))%5
	else
	set @work = @date+(6-@wk)+7*((@nd-(6-@wk))/5)+(@nd-(6-@wk))%5+2
	end
	if @nd <= (6-@wk)
	begin
	set @work = @date+@nd
	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)+1+@nd%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)+@nd%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 > (6-@wk)
	begin
	if (@nd-(6-@wk))%5 = 0
	set @work = @date+(6-@wk)+7*((@nd-(6-@wk))/5)+(@nd-(6-@wk))%5
	else
	set @work = @date+(6-@wk)+7*((@nd-(6-@wk))/5)+(@nd-(6-@wk))%5+2
	end
	if @nd <= (6-@wk)
	begin
	set @work = @date+@nd
	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)+1+@nd%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)+@nd%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

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.