Link to home
Start Free TrialLog in
Avatar of TECH_NET
TECH_NET

asked on

SQL Query to detech any overlap between 2 time zone range

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
Avatar of GLoad
GLoad

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

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.
*field*
Avatar of TECH_NET

ASKER

I do not use the date in the comparison. I need to use the way it is currently stored.
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
--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
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.

ASKER CERTIFIED SOLUTION
Avatar of stevebobs
stevebobs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial