See if a datetime is between another records datetime

Posted on 2012-12-28
Last Modified: 2013-01-03
Hopefully I can explain myself on this one.

I work for a trucking company and we trying to see if we have overlapping times.

I have a query that tells me the StartTime and EndTime for trips based on a specific vehiicle that performs the services.   These are two columns and look something like so:

Order             StartTime                                          EndTime
123                 2012-12-28 11:29:26.000                2012-12-28 13:41:02.000
344                 2012-12-28 08:35:15.000                2012-12-28 11:10:53.000
xyx                  2012-12-28 11:45:26.000                2012-12-28 11:59:26.000

I would like to add a column that checks for overlapping times.  So lets say I have an Order xyx and the start time falls within that of the times already indicated, then have a column that basically flags it using a SELECT Query.

An example would be lets say Order 'xyz' above has the start time of '2012-12-28 11:45:26.000' well that would be flagged, because using the example I have above it can't be because during that time, Order 123 was being performed.  Same would go for the End Time.

Just trying to figure out an easy way of identify those that overlap because my trucks can't be at two different places at the same time if that makes sense?
Question by:chrisryhal
LVL 45

Assisted Solution

Kdo earned 250 total points
ID: 38728115
Hi Chris,

A pretty good starting point is to just query where the start or end date appears in another record's range.

SELECT * FROM orders o
INNER JOIN orders o1
  ON o.startdate between o1.startdate and o1.enddate
SELECT * FROM orders o
INNER JOIN orders o1
  ON o.enddate between o1.startdate and o1.enddate;


SELECT * FROM orders o
INNER JOIN orders o1
  ON o.startdate between o1.startdate and o1.enddate
  OR o.enddate between o1.startdate and o1.enddate;

Either of those should be a great start!

Good Luck,
LVL 69

Accepted Solution

Scott Pletcher earned 250 total points
ID: 38728207
First, it will really help performance to have an index on: ( StartTime, EndTime, [Order] )


SELECT o1.*, o2.*
FROM dbo.Orders o1
INNER JOIN dbo.Orders o2 ON
    o2.[Order] > o1.[Order] AND NOT (o2.StartTime > o1.EndTime OR o2.EndTime < o1.StartTime)

Using the sample test data:

;with orders as (
    select '123' as [order],      '2012-12-28 11:29:26.000' as starttime, '2012-12-28 13:41:02.000' as endtime union all
    select '344',                 '2012-12-28 08:35:15.000'             , '2012-12-28 11:10:53.000' union all
    select 'xyx',                 '2012-12-28 11:45:26.000'             , '2012-12-28 11:59:26.000'
SELECT o1.*, o2.*
FROM Orders o1
INNER JOIN orders o2 ON
    o2.[Order] > o1.[Order] AND NOT (o2.StartTime > o1.EndTime OR o2.EndTime < o1.StartTime)

Author Closing Comment

ID: 38740685
Thanks for both of your help and sorry for my delay.

I was able to get it to work with the help of both of you :)

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question