Solved

MSSQL Datediff for time slot scheduling

Posted on 2009-06-30
2
675 Views
Last Modified: 2012-05-07
I am in making a process for class registration. Each class has a corresponding time slot id, of which has a timeslotstart and timeslotend column. Currently they are varchar values since they only are hour & minute time slots. My issue is, when a person signs up for a class in timeslotid 2, starttime being 10 am endtime being 11am, I need to be able to remove other classes from the selection that have a starttime or endtime that is between those dates. I have something that sort of works below, but I am hoping to find something a little better. With the example below, it works for all entries except for ID 13, which has a start time of 9AM and end time of 17PM(5PM).
---------query

SELECT timeslotid, 

datepart(hh, convert(smalldatetime, timeslotstart, 14)) as StartTime, 

convert(smalldatetime, timeslotend, 14) as EndTime,

CASE 

WHEN datepart(hh, convert(smalldatetime, timeslotstart, 14)) 

between 

(SELECT datepart(hh, convert(smalldatetime, timeslotstart, 14)) 

from dbo.TimeSlots WHERE TimeSlotID = 2)

and 

(SELECT datepart(hh, convert(smalldatetime, timeslotend, 14))from dbo.TimeSlots WHERE TimeSlotID = 2)

THEN 'NOT'

ELSE 'SO'

END as StartTime,

CASE 

WHEN convert(smalldatetime, timeslotend, 14) 

between 

(SELECT  convert(smalldatetime, timeslotstart, 14) 

from dbo.TimeSlots WHERE TimeSlotID = 2)

and 

(SELECT convert(smalldatetime, timeslotend, 14)from dbo.TimeSlots WHERE TimeSlotID = 2)

THEN 'NOT'

ELSE 'SO'

END as Endtime

from dbo.timeslots

 

 

----results   bs= between start       be=between end

timeID		start			end		bs	be

1		9		1900-01-01 10:00:00	SO	NOT

2		10		1900-01-01 11:00:00	NOT	NOT

3		11		1900-01-01 12:00:00	NOT	SO

4		14		1900-01-01 15:00:00	SO	SO

5		15		1900-01-01 16:00:00	SO	SO

6		16		1900-01-01 17:00:00	SO	SO

7		9		1900-01-01 10:30:00	SO	NOT

8		9		1900-01-01 11:00:00	SO	NOT

9		10		1900-01-01 12:00:00	NOT	SO

10		10		1900-01-01 11:30:00	NOT	SO

11		14		1900-01-01 15:30:00	SO	SO

12		14		1900-01-01 16:00:00	SO	SO

13		9		1900-01-01 17:00:00	SO	SO

14		10		1900-01-01 12:00:00	NOT	SO

Open in new window

0
Comment
Question by:GLIanimal
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24746127
Try something like this:
+Uses a common table expression to simplify tests requiring the start and end times as datetime data type (could use view or do conversions in line).
+Filter records to time slots not equal to one already selected, in your case id #2.
+Apply or join in the data for the time slot that is selected (#2).
+Test that start is not between start and end time of selected record; however, since can start a class right as one ends backoff the endtime by a minute since between is inclusive.
+Repeat test for end time with offset to start of next class.
+Repeat with opposite side as current class may be much longer and span over already selected class.
with timeslotCTE AS (

	select timeslotid

	, cast(start as datetime) as StartTime

	, cast([end] as datetime) as EndTime

	from timeslots

) select t1.timeslotid

, convert(varchar, t1.starttime, 108) as StartTime

, convert(varchar, t1.endtime, 108) as EndTime

from timeslotCTE t1

cross apply (select starttime, endtime from timeslotCTE where timeslotid = 2) t2

where t1.timeslotid <> 2

	and t1.starttime not between t2.starttime and dateadd(mi, -1, t2.endtime)

	and t1.endtime not between dateadd(mi, +1, t2.starttime) and t2.endtime

	and t2.starttime not between t1.starttime and dateadd(mi, -1, t1.endtime)

	and t2.endtime not between dateadd(mi, +1, t1.starttime) and t1.endtime

;

Open in new window

0
 

Author Closing Comment

by:GLIanimal
ID: 31598338
Had to add IN and count for multiple values, but all in all thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modifying SQL 2008/2012 PARTITIONS 3 55
Set the max value for a column 7 37
configure service broker on all databases 2 76
Query 14 54
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

23 Experts available now in Live!

Get 1:1 Help Now