Here's something I knocked up which sort of does what you want. Trouble is, you're trying to select timeslots that don't exist. So you need a query that will generate a list of timeslots, and then filter out the timeslots where instructors are scheduled for classes. This code does that. It has some hard coded values, and isn't intended to be used as is. It's just a base, something to get you started. I've included a temp table based on your table, and a script to populate it.CREATE TABLE #CourseSchedule ( Course_ID INT, Weekday VARCHAR(5), start_time DATETIME, End_time DATETIME, Instructor INT)INSERT INTO #CourseSchedule SELECT 1, 'Mon', '20100412 09:00:00', '20100412 12:00:00', 1UNION ALL SELECT 1, 'Mon', '20100412 13:00:00', '20100412 16:00:00', 1UNION ALL SELECT 2, 'Tue', '20100413 09:10:00', '20100413 09:20:00', 2UNION ALL SELECT 2, 'Tue', '20100413 14:00:00', '20100413 17:00:00', 2UNION ALL SELECT 2, 'Wed', '20100414 09:00:00', '20100414 12:00:00', 2UNION ALL SELECT 2, 'Wed', '20100414 14:00:00', '20100414 17:00:00', 1UNION ALL SELECT 2, 'Thu', '20100415 09:00:00', '20100415 12:00:00', 2UNION ALL SELECT 2, 'Thu', '20100415 14:00:00', '20100415 17:00:00', 1UNION ALL SELECT 2, 'Fri', '20100416 09:00:00', '20100416 12:00:00', 2UNION ALL SELECT 2, 'Fri', '20100416 14:00:00', '20100416 17:00:00', 1DECLARE @InitStartDate DATETIME SET @InitStartDate = '20100412 09:00:00';WITH mt AS ( SELECT StartTime, DATEADD(mi, 30, StartTime) AS EndTime FROM ( SELECT DATEADD(mi, Number * 30, @InitStartDate) AS StartTime FROM master..spt_values WHERE Number < 210 AND Type = 'P') z WHERE CONVERT(VARCHAR, StartTime, 8) BETWEEN '09:00:00' AND '17:30:00' )SELECT DATENAME(dw, mt.StartTime), mt.StartTime, mt.EndTimeFROM mtLEFT JOIN #CourseSchedule cs ON mt.StartTime < cs.End_time AND mt.EndTime > cs.start_timeWHERE cs.End_time IS NULL DROP TABLE #CourseSchedule
------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee.