Solved

Get Number of Holidays between two dates

Posted on 2014-02-11
4
896 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
[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
4 Comments
 
LVL 66

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 41

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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