Solved

Get Number of Holidays between two dates

Posted on 2014-02-11
4
839 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

13 Experts available now in Live!

Get 1:1 Help Now