简体   繁体   中英

Given a date range, find events that overlap within that range

Given a input date range, such as 11/1/2015 - 11/15/2015, what is the most efficient way to determine which events(CalendarEvents) are going on during that given date range.

event         eventStart     eventEnd
==================================
expo          10/25/2015    11/4/2015       //This should be selected.

concert       11/4/2014      11/5/2015      //This should be selected.

exhibit       11/15/2015     12/1/2015      //this should be selected.

display       10/26/2015    10/29/2015      //this should NOT be selected.

Linq or SQL server would be awesome. Basically given a date range, find events that overlap within that range.

I know I could "brute force" with a bit of code, just wondering if I'm missing something more elegant?

You can use StartA <= EndB AND EndA >= StartA to get the overlapping dates:

DECLARE @startDate  DATE = '20151101',
        @endDate    DATE = '20151115'
SELECT * 
FROM CalendarEvents
WHERE
    eventStart <= @endDate
    AND eventEnd >= @startDate

SQL Fiddle

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM