I like to use table variables so I have just produced a list of units where the dates conflict
I have put the room data into an incremental id'ed table with dates then checked them all against each other
I then create the conflict table show which id's conflict.
declare @roomdates table ( id int identity(1,1), roomid varchar(6) , fromD date , toD date, conflicted bit)
declare @tableConflict table( idA int ,idb int )
insert into @roomdates (roomid , fromd, toD , conflicted)
values('101', '2020-02-17','2020-02-22', 0)
insert into @roomdates (roomid , fromd, toD , conflicted)
values('101', '2020-02-14','2020-03-22', 0)
insert into @roomdates (roomid , fromd, toD , conflicted)
values('101', '2020-03-11','2020-03-14', 0)
insert into @roomdates (roomid , fromd, toD , conflicted)
values('101', ' 2020-04-11 ',' 2020-04-14', 0)
select * from @roomdates
declare @min int = 0, @max int = 0
select @min = min(id) , @max = max(id) from @roomdates
declare @d1 date , @d2 date
while @min <= @max
begin
if @min = 500
begin
break
end
select @d1 = fromd , @d2 = toD from @roomdates where id = @min
insert into @tableConflict
select @min, id from @roomdates where @d1 >= fromD and @d2 <= toD and id ! = @min
select @min
set @min = @min + 1
end
select * from @tableConflict