in reply to Trying to select all records that match a datetime range.

swares:

If I understand you correctly, you have several cases you need to cover, like so:

Desired range: +---------------+ Case 1,2: +-1-+ +---2--+ Case 3,4: +----3-----+ +--------4-----------+ Case 5: +---5----+ Case 6: +------------------6----------------+
and you want to return records for cases 3, 4, 5, and 6, but you're not getting case 6. I normally cover these cases by:

  • Look for a cases start time and/or stop time inside the desired range.
  • Look for the desired ranges start and/or end time inside the cases range.
  • That should catch all the cases for you. The SQL code I normally use resembles:

    select * from table where (start_time between desired_start and desired_stop) or (end_time between desired_start and desired_end) or (desired_start between start_time and end_time) or (desired_stop between start_time and end_time)
    ...roboticus