Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL calculation should add working days only (i.e. exclude weekend and public holidays)

Posted on 2009-03-30
2
Medium Priority
?
1,679 Views
Last Modified: 2012-05-06
I have a select statement that currently works like below:

select inwp.cpp_quotation_days + cp.cp_date_created as [Date Quote Due]

cpp_quotation_days  come from a table for this example lets say quotation days = 20
If i add 20 to the cp_date_created field I get the Date Quote Due.

A new requirement is for the calculation to only add working days to get the Date Quote Due (exluding weeknd and public holidays)

Any ideas?
0
Comment
Question by:johnny_32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 2000 total points
ID: 24021153
Only way I know how is to create a holidays type table. This is the approach taken for msaccess as well.

Have a look here at some udf's to work this way
http://sql-server-performance.com/Community/forums/t/20578.aspx
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24022030

--First create a Holiday table which holds the holidays.
 
create table Holidays(datefield datetime,holiday nvarchar(100),country nvarchar(100))
insert into Holidays
select '12/25/2009', 'christmas', 'usa'
 union all
select '12/26/2009', 'Day After Christmas', 'USA'
 union all
select '05/25/2009', 'Memorial Day', 'USA'
--------------------------------------------------------------------------------------------------------
--Then create a function like this.
create function dbo.fn_GetWorkingDays 
(@Date datetime,
 @Days int) returns int as
begin
declare @table table(datefield datetime)
declare @StartDate datetime, @EndDate datetime
select @StartDate = @date,@EndDate = DATEADD(DD,@days,@date)
while @StartDate < @EndDate
begin
insert into @Table values(@StartDate)
set @StartDate = @StartDate + 1
end
 
select COUNT(*)
  from @Table
 where DATEPART(dw,datefield) not in (1,7)
   and dateadd(dd,0,datediff(dd,0,datefield)) not in (select dateadd(dd,0,datediff(dd,0,datefield)) from Holidays)
drop table Holidays
end
--------------------------------------------------------------------------------------------------------
-- And finally call this function in your SELECT query.
select dbo.fn_GetWorkingDays(cp.cp_date_created,inwp.cpp_quotation_days) as [Date Quote Due]

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question