Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

See if a datetime is between another records datetime

Posted on 2012-12-28
3
168 Views
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?
0
Comment
Question by:chrisryhal
3 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen 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
UNION ALL
SELECT * FROM orders o
INNER JOIN orders o1
  ON o.enddate between o1.startdate and o1.enddate;

similary:

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,
Kent
0
 
LVL 69

Accepted Solution

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

Then:

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)
0
 
LVL 2

Author Closing Comment

by:chrisryhal
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 :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

808 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