Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Jul 18, 2003

Add 'n' working days to a given date

By DatabaseJournal.com Staff

>>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



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date