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.
with t(DateFrom, DateTo, Price, Type) as (
select convert(date, '2019-05-25'), convert(date, '2019-12-31'), 1000, 'Price' union all
select '2019-05-26', '2019-12-31', 800, 'Discount' union all
select '2020-01-01', '2020-12-31', 1100, 'Price' union all
select '2020-07-05', '2020-09-30', 900, 'Discount'
)
select t.*
from (select v.dte as datefrom, dateadd(day, -1, lead(v.dte) over (order by v.dte)) as dateto, v.price, v.type
from t join
t tfull
on tfull.type = 'Price' and
tfull.datefrom <= dateadd(day, 1, t.dateto) and
tfull.dateto >= dateadd(day, 1, t.dateto) cross apply
(values (t.datefrom, t.price, t.type),
(dateadd(day, 1, t.dateto), tfull.price, tfull.type)
) v(dte, price, type)
) t
where dateto >= datefrom
order by dateto

datefrom dateto price type
25/05/2019 00:00:00 25/05/2019 00:00:00 1000 Price
26/05/2019 00:00:00 31/12/2019 00:00:00 800 Discount
01/01/2020 00:00:00 04/07/2020 00:00:00 1100 Price
05/07/2020 00:00:00 30/09/2020 00:00:00 900 Discount