I had a problem but part was solving thanks to this question, but know i want to know how can i count the days beetween a given interval.
It is a query to count how many days an employee goes to vacation. So given (or not given) a date range, i would like count how many vacation days are beetween given interval.
Currenty my SQL is like this:
SELECT SUM(DATEDIFF(vacation_end_date,vacation_start_date))
FROM vacation WHERE vacation_end_date >= given_start_date
AND vacation_start_date <= given_end_date'
I know thatthe DATEDIFF will only work for days inside given range, but it shouldn’t count days outside from given range.
Please help me, couldn’t find something similar on Stack Overflow.
If vacation_end_date is the day the person is back from vacation and not his last day vacationing, the query to get the person’s vacation days between 2012-01-01 (inclusive) and 2013-01-01 (exclusive) would be;
If vacation_end_date is his last vacation day, you’ll need to replace vacation_end_date with
ADDDATE(vacation_end_date, INTERVAL 1 DAY))in the query above to get the calculation correct.Simple demo here.