add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table Table1 (ID varchar(1), d1 date, d2 date);
insert into table1 values
('A', '2023-01-01', '2023-10-31'),
('B', '2023-01-01', '2023-03-31');

create table Table2(ID varchar(1), d1 date, d2 date);
insert into table2 values
('A', '2023-02-01', '2023-09-30'),
('B', '2023-03-01', '2023-04-30');
4 rows affected
select t.id, t.d1, t.d2 from (
select id, dt d1, lead(dt) over (partition by id order by dt) d2
from (
select id, d1 dt from table1 union select id, d2 from table1 union
select id, dateadd(day, -1, d1) d1 from table2 union
select id, dateadd(day, 1, d2) d2 from table2) u ) t
left join table2 a on a.id = t.id and a.d1 < t.d2 and t.d1 < a.d2
where a.id is null and t.d2 is not null

id d1 d2
A 2023-01-01 2023-01-31
A 2023-10-01 2023-10-31
B 2023-01-01 2023-02-28