Solved

Get Number of Holidays between two dates

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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