Search code examples
sqlsql-servert-sqlsql-server-2000

How to find all fridays and holidays between two dates


The table:

hDate      Holiday

17/12/2011 National Day
01/01/2012 New Year
....

From the table, i want to find the total number of holidays between two dates:

A query like:

select count(hdate)
from table1
where hdate between '" & start_date & "' and '" & end_date & "'

User input:

start_date = '16/12/2011' 
end_date = '15/01/2012' 

and also I want to find the friday between 2 dates.

For finding fridays, how to create a query?

Expected output:

Holiday Friday

2       5

[2] - 2 days holiday from table1, [5] - 5 days friday

How to do this?


Solution

  • This counts the fridays between 2 dates:

    declare @from datetime= '2012-01-26'  
    declare @to datetime  = '2012-01-28'
    
    select datediff(day, -3, @to)/7-datediff(day, -2, @from)/7 
    

    The holidays are easy to find, it seems like you have that part covered already.

    I sort of answered this earlier. But didn't get any credit:

    How to calculate the number of "Tuesdays" between two dates in TSQL?