0

I need to get the number of holidays between two dates. I have tried using the query below but I'm getting an incorrect count number.

StartDate EndDate
01/2/2022 03/2/2022
17/2/2022 19/2/2022
SELECT COUNT(*) FROM table
WHERE StartDate <= '02/2/2022' and EndDate >= '19/2/2022'

how I make this date => '02/2/2022' return count of day that between two dates from the first row from the table and get count day from the second row.

The count must be 5 days.

4
  • Isn't it easier to search before asking a redundant question. Q: tsql get hollydays between 2 dates => stackoverflow.com/questions/12803823/… Commented Apr 14, 2022 at 14:21
  • 1
    @Fildor I deleted my incorrect comments. Commented Apr 14, 2022 at 15:12
  • 1
    @AndrewTruckle No, it's not clear to me, if you see the table and query you find select must be started from '2/2/2022', and this date found between '1/2/2022' and '3/2/2022', Commented Apr 14, 2022 at 15:50
  • I often try to get the query to work as desired in Access Query Interface. Ince it works there I transfer into my software however appropriate. Commented Apr 14, 2022 at 18:05

1 Answer 1

0

The simplest way of doing it is to have a table that has one row per day for every day from eg year 2000 to year 2099, then you can:

SELECT COUNT(*) 
FROM
  cal           --your dates table
  INNER JOIN h. --your holiday table 
  ON cal.d BETWEEEN h.start AND h.end
WHERE
  cal.d BETWEEN '2022-02-02' AND '2022-02-19'

The join will produce 6 rows, the where trims it to 5, there is your count

Generating the calendar table, or its equivalent (an arbitrary series of dates) is an exercise for the reader.. See something like this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.