Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come posso determinare in SQL Server se un intervallo dateTime si sovrappone a un altro

Puoi fare così

declare @T table (ID int, startDateTime datetime, endDateTime datetime)

insert into @T values
(  1 , '2010-01-01 10:30:00' , '2010-01-01 11:00:00'),
(  2 , '2010-01-01 10:30:00' , '2010-01-01 11:30:00'),
(  3 , '2010-01-01 11:00:00' , '2010-01-01 11:30:00'),
(  4 , '2010-01-01 11:00:00' , '2010-01-01 12:00:00'),
(  5 , '2010-01-01 11:30:00' , '2010-01-01 12:00:00')

declare @startDateTime datetime
declare @endDateTime datetime


set @startDateTime = '2010-01-01 11:00:00'
set @endDateTime = '2010-01-01 11:30:00'

select *
from @T
where
  startDateTime < @endDateTime and
  endDateTime > @startDateTime