Solved

Comparing date times start time and end time using between

Posted on 2010-09-09
6
349 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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Encryption question 2 61
Isolation level in SQL server 3 52
Following an example - removing duplicate strings 4 61
Query group by data in SQL Server - cursor? 3 47
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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