Solved

SQL Query to detech any overlap between 2 time zone range

Posted on 2010-08-25
8
686 Views
Last Modified: 2012-05-10
I have a  sql procedure that needs to check for any overlaps between 2 time ranges. ie if u look at the form attached. You will see that the START time options START_HOUR and START_MINUTE
and the END time option is END_HOUR and END_MINUTE.

now for illustration purpose, let me assume that the START_HOUR parameter that is sent to the SQL stored procedure is 8 and START_MINUTE is 1 (for 00) . Similarly the END_HOUR is 9 and END_MINUTE is 1(for 00). So the time range is 8:00 - 9:00

Now i want to check if any time range choosen falls in this range.(ie in between 8:00 and 9:00)

This can happen to any list choosen. Hence if the user choose the Input form and enter 8:15 - 9:00, the store procedure should tell me that a overlap has occured.

How do i handle such situation and check for overlaps.
8-25-2010-9-35-19-PM.png
0
Comment
Question by:TECH_NET
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 2

Expert Comment

by:GLoad
ID: 33527760
You will need to ammend this query to use your variables, but this is the principal.

You should also ensure your dates are comparible and are stored as datetimes on the same day or serial times (in seconds or something).

If the count on this is > 0 you have a conflict. This will cover you for times that start and finish inside the entered, start and finish before and after the entered or overlap in any way.


SELECT
  COUNT(*)
FROM
  SESSIONS
WHERE
  (@StartDate BETWEEN SESSIONS.STARTDATE AND SESSIONS.ENDDATE)
  OR (@EndDate BETWEEN SESSIONS.STARTDATE AND SESSIONS.ENDDATE)
  OR (SESSIONS.STARTDATE BETWEEN @StartDate AND @EndDate)
  OR (SESSIONS.ENDDATE BETWEEN @StartDate AND @EndDate)

Open in new window

0
 
LVL 8

Expert Comment

by:infolurk
ID: 33527777
Looks like you want to set a trigger.

Using the between function in a query should do the trick i.e
select session identifier
from sessions table s
 join
start/finish times table sf
where sf.start between s.start and s.finish or sf.finish between s.start and s.finish

It would be simpler if you had the start and finish times in the sessions table in seperate fields. Otherwise you will have to create variables to grab the required information from the one fiels.
0
 
LVL 8

Expert Comment

by:infolurk
ID: 33527778
*field*
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:TECH_NET
ID: 33527863
I do not use the date in the comparison. I need to use the way it is currently stored.
0
 

Author Comment

by:TECH_NET
ID: 33527894
infolurk: I do have the start and finish times in the sessions table in seperate columns. Refer to the attached image.

In your query u have used start/finish timestable. what s this table.?
8-25-2010-11-22-27-PM.png
0
 
LVL 2

Expert Comment

by:stevebobs
ID: 33538749
--Now i want to check if any time range choosen falls in this range.(ie in between 8:00 and 9:00)

i think it would be cleaner to convert the daterange (like 1:00-2.15)  to a number range first.
Also you need to specify whether the comparison is inclusive or not, ie most users would enter 1:00 - 2:00 and 2:00-3:15  in preference to 1:00 - 1:59 and  2:00-3:14

Could use CAST or CONVERT functions but these can be fiddly; i tend to use the most readable and least fussy option...
ie
something like

-- input vars, hours and dates
@sth INT
@stm INT
@fth INT
@ftm INT

--intermediate vars
DECLARE @startdate INT
DECLARE @finishdate INT

--make into number
@startdate = @sth*60 + @stm
@finishdate = @fth*60 + @ftm

-- then can compare using normal numeric operators <, <=, >=, BETWEEN etc to compare against other stored dates...

--
Note: I would also store existing calculated values in a calculated column for the comparison values rather than converting for each check - also easier to debug this way as mathematical issues are generally unambiguous
0
 

Author Comment

by:TECH_NET
ID: 33538922
stevebobs: I had indeed progressed a similar way but adding a new column in the view which converts the 9:10-10:30 AM  as 9*60 + 10 for Morning sessions and 9*60 + 720 + 30 for 9:00 -10:30 PM for the evening session.

Note: (720=12*60)

Now the problem is with 2 additional columns with computed minutes, how do i do the comparison.

eg:
for 8:05 AM to 09:00 AM the START_TIME column value would be  485 and the END_TIME would be 540

now i choose another entry

8:40 AM - 9:40 AM which we know clearly overlaps the previous time range.

Our calculated results would be START_TIME column value=520 and END_TIME column value=580


SELECT COUNT(*) AS TOTAL FROM TIMETABLE_VIEW
WHERE
( START_TIME   BETWEEN @START_TIME_ID AND  @END_TIME_ID ) OR
( END_TIME   BETWEEN @START_TIME_ID AND  @END_TIME_ID )  

WHERE START_TIME_ID would be 520 and END_TIME_ID=580


This query fails. I am stuck here.

0
 
LVL 2

Accepted Solution

by:
stevebobs earned 500 total points
ID: 33539335
-- surely this would suffice...?
SELECT COUNT(*) AS TOTAL FROM TIMETABLE_VIEW
WHERE
( START_TIME   >= @START_TIME_ID)  AND (END_TIME < @END_TIME_ID )

0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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