How can I determine in SQL Server if a dateTime range overlaps another

前端 未结 1 999
北荒
北荒 2020-11-29 13:16

We have eg. following table

| ID | startDateTime       | endDateTime         |
+----+---------------------+---------------------|
|  1 | 2010-01-01 10:30:00          


        
相关标签:
1条回答
  • 2020-11-29 13:31

    You can do like this

    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
    
    0 讨论(0)
提交回复
热议问题