?
Solved

Get Number of Holidays between two dates

Posted on 2014-02-11
4
Medium Priority
?
922 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

800 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