You can run a query like this one:
select count(*) as cnt
from reservations t
where
not
(
(@dtTo <= t.DateFrom)
or
(@dtFrom >= t.DateTo)
)
assuming that @dtFrom, @dtTo are the dates the second user has chosen, and assuming
you have a reservations
table with DateFrom
and DateTo
columns.
- If this query returns 0, then allow the second user to save.
- If it returns a number > 0, then don't allow the second user to save.
What's the idea behind this:
two segments of time [dateFrom1, dateTo1]
and [dateFrom2, dateTo2]
are not
conflicting (i.e. are not overlapping) for this reservation-based scenario if and only if:
dateTo1 <= dateFrom2 or dateFrom1 >= dateTo2
(draw it on a piece of paper and you'll see why).
So if the negation
not ( dateTo1 <= dateFrom2 or dateFrom1 >= dateTo2 )
is true, the two segments are overlapping/conflicting.