Solved

Comparing date times start time and end time using between

Posted on 2010-09-09
6
354 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
[X]
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
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 143

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 143

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

636 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