Solved

Comparing date times start time and end time using between

Posted on 2010-09-09
6
343 Views
Last Modified: 2012-05-10
I cant seem to get the right combination to generate the proper results.  I need to compare the start date and end date to determine if rows are overlaping.  Example

RowID        StartTime                           EndTime
1                9/11/2010  08:00:00         9/11/2010  09:00:00
2                9/11/2010  07:30:00         9/11/2010  08:30:00
3                9/11/2010  08:30:00         9/11/2010  09:30:00
4                9/11/2010  09:30:00         9/11/2010  10:30:00

I need to return row 1,2 and 3 but not row 4 since the start time is the same as the end time of the previous appointment. Any help is greatly appreciated.
0
Comment
Question by:clintnash
6 Comments
 
LVL 20

Expert Comment

by:dsacker
ID: 33638162
Try this:
select RowID,
       StartTime,
       EndTime
from   MyTable
where  StartTime NOT IN (select EndTime from MyTable)

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33638280
what about this:
select t.*

 from yourtable t

 where exists( select null from yourtable o where o.starttime < t.endtime and o.endtime > t.starttime )

Open in new window

0
 
LVL 3

Accepted Solution

by:
mehdi_javan earned 500 total points
ID: 33638466
I think you need this:

Select t1.* 
from YourTable t1
where exists (select null from YourTable t2
              where (t1.StartTime > t2.StartTime and t1.StartTime < t2.EndTime)
              or (t1.EndTime > t2.StartTime and t1.EndTime < t2.EndTime)
              or (t1.StartTime < t2.StartTime and t1.EndTime > t2.EndTime)

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Closing Comment

by:clintnash
ID: 33638666
Short of a missing ) at the end, this query returns the correct results. Thanks to each of your for your help this morning.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33638677
did you try mine? did it not work?
0
 
LVL 1

Author Comment

by:clintnash
ID: 33639633
Angel, Actually I tried them all, the first returned two of the three rows, yours returned all four rows.  Thank you...
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now