Solved

Get Number of Holidays between two dates

Posted on 2014-02-11
4
823 Views
Last Modified: 2014-02-12
I made a table with all our holiday days
SELECT     COUNT(ID) AS NumberOfHolidays
FROM         Holidays
WHERE     (HolidayDate > { fn NOW() })

Open in new window


I have a table with Projects and each has a different end date.  I want to use the dataset but instead of it doing > Now I want it to be Between Now() and EndDate.  I can't use a parameter because it changes for each row (unless someone knows how to change the value of a parameter for each row of a table).  I thought I could use a variable but I'm not sure how to do that either.  I wanted to use it like a Lookup but it's not working out as well as I hoped.  If I could put it in my function that would be even better because I'm passing the start and end dates to it.

Function getBusinessDaysCount(ByVal tFrom As Date, ByVal tTo As Date) As Integer 
Dim tCount As Integer
Dim tProcessDate As Date = tFrom 
For x as Integer= 1 To DateDiff(DateInterval.Day, tFrom, tTo) + 1 
If Not (tProcessDate.DayOfWeek = DayOfWeek.Saturday Or tProcessDate.DayOfWeek = DayOfWeek.Sunday) Then tCount = tCount + 1
tProcessDate = DateAdd(DateInterval.Day, 1, tProcessDate) 
Next 
Return tCount 
End Function

Open in new window


I got that code off the internet.  It successfully removes Saturdays and Sundays but I need to account for stat holidays as well as mandatory vacation days (which I have put into a Holidays table).  

I can't do the calculation in my initial query because I have to grab the EndDate from a separate table and it's a varchar.
 ISNULL((SELECT     CONVERT(datetime, CltCustValue, 101) AS Expr1
                              FROM         CltCustom
                              WHERE     (CltCustCltId = Clients.ID)), 0) AS E_End_Date

Open in new window


Any suggestions are greatly appreciated!!!!
0
Comment
Question by:HSI_guelph
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39851553
< On a conference call, so this comment is a 'drive by'.  Sorry. >

Here's an article on How to build your own calendar table to pull that off.   The reason this is a custom dang deal is that holidays are not something that can be determined using available SQL Server functions.
0
 

Author Comment

by:HSI_guelph
ID: 39851597
@Jim Horn - I've seen that article but I already have a table with all the dates in it so I'm not sure how that can help with my current table.  
Holiday table
Plus I need to grab this for each row and each row has a different end date.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39851650
Check if this works
SELECT *, 
       (SELECT COUNT(ID) 
          FROM Holidays 
         WHERE HolidayDate > GETDATE() 
               AND HolidayDate < t1.CltCustValue) AS NumberOfHolidays 
  FROM CltCustom t1 

Open in new window

Otherwise post some sample data from your tables and expected result.
0
 

Author Closing Comment

by:HSI_guelph
ID: 39854397
Changed it up to
(SELECT     COUNT(ID) AS NumberOfHolidays
                            FROM          Holidays
                            WHERE      (HolidayDate BETWEEN { fn NOW() } AND CltCustom_1.CltCustValue)) AS HolidayDays
and worked like a charm.  It gives me the days I need to subtract from the Business Days and all calculations are working well now.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now