In my DB collection (Appery based on Mongo DB) I have startDate and EndDate columns (date range).
Before I want to insert new record to my collection I want to check if my new date range overlap the existing data range in table, so in case its not I will create new record.
Let say that in collection I have the following record:
StartDate = 2014-06-29 18:00:00.000
EndtDate = 2014-06-29 22:00:00.000
Now I want to add new record that:
StartDate = 2014-06-29 16:00:00.000
EndtDate = 2014-06-29 19:00:00.000
and in this case I don't want to create new record since this overlap the existing data in DB
*There are 4 combination to overlap and two that not
Overlap:
Inside the range
StartDate = 2014-06-29 19:00:00.000
EndtDate = 2014-06-29 20:00:00.000All over the range
StartDate = 2014-06-29 17:00:00.000
EndtDate = 2014-06-29 23:00:00.000before and after the start date only:
StartDate = 2014-06-29 17:00:00.000
EndtDate = 2014-06-29 19:00:00.000before and after the end date only:
StartDate = 2014-06-29 19:00:00.000
EndtDate = 2014-06-29 23:00:00.000In addition:
*There are 2 combination to events that not overlap existing dates:All date range before StartDate
StartDate = 2014-06-29 12:00:00.000
EndtDate = 2014-06-29 17:00:00.000All date range after EndDate
StartDate = 2014-06-29 23:00:00.000
EndtDate = 2014-06-29 23:30:00.000How can we do this SQL to validate all combination above?