Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have one table in sql

SQL
Date            Day     Start Time  End Time  Subject   Teacher
2014-06-02	Monday	2:30 PM	    04:30 PM  Maths	Mr.Smith
2014-06-10	Tuesday	2:30 PM	    04:30 PM  Science   Mr.Ayar
2014-06-16	Monday	2:30 PM	    04:30 PM  Maths	Mr.Smith
2014-06-02	Monday	3:30 PM	    04:30 PM  Maths	Mr.Smith Inserted New Row


I want to make query to check Mr.Smith is already engaged in inserted time slot
Posted
Updated 4-Jun-14 0:19am
v3

If either the begin or the end of your time slot is between (that particular day) 'Start Time' and 'End Time' then that person is already engaged. It is not a difficult query to write.
 
Share this answer
 
Member 10272815....i hope below query will clarify your raised overlap.

SQL
SELECT COUNT(1) FROM Your_Table (NOLOCK) WHERE teacher = 'Mr.Smith' And Day = 'Monday'
AND [Start Time] BETWEEN 'Your Start Time' AND 'Your End Time' OR [END Time] BETWEEN 'Your Start Time' AND 'Your End Time'
 
Share this answer
 
Hi,

Put a check before inserting a new record. If it returns more than 0 output than record for specified criteria is available else not.

Check query is below

SELECT COUNT(1) FROM Your_Table (NOLOCK) WHERE teacher = 'Mr.Smith' And Day = 'Monday'
AND 'Your Start Time' BETWEEN [Start Time] AND [END TIME]


Hope this will help you.
 
Share this answer
 
Comments
Member 10272815 4-Jun-14 10:22am    
That only covers one of three potential overlaps. New start time can fall inside an existing appointment, new end time can fall inside an existing appointment, or the new appointment can entirely encompass an existing appointment.

If new start time is before existing end time, and new end time is after existing start time then you should find all conflicts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900