where clause with time columns on sql server
In the project there a functionality that lets you book the place during the day, they have to enter the day, the start time and final time from 7:00 am to 12:00 AM (midnight) or 1:00 AM, for example if someone enter date=21/oct/2011 start time=8:00 pm and end time 12:00 am (he book the place from 8:00 pm until 12:00 am) the webform send to the store procedure 20:00 and 00:00 to check the table to see if is available, if someone already book in the same day until midnight it is store like this startime=23:00 endtime=00:00
开发者_StackOverflowso when i check the new client it has to return that there a reservation already in the range of time, my query is not efficient but it working from the 7:00 to 23:00 range, it fails when from the webfrom enters a endtime 12 am (00:00 on sql) because the starttime es greater than the end time
this is my query
select COUNT(*)
from table1
where id_place=@id_place
and date=@date
and (
(@start_time=res_start_time and @end_time=res_end_time)
or (@start_time > res_start_time and @start_time < res_end_time)
or (@end_time > res_start_time and res_end_time < res_end_time)
or (res_start_time > @start_time and res_start_time < @end_time)
or (res_end_time > @start_time and res_end_time < @end_time)
or (res_start_time < @start_time and res_end_time >@end_time)
)
-- @start_time = start time of the reservations (from webform)
-- @end_time = end time of the reservations (from webform)
-- res_start_time= represents the start time column
-- res_ebd_time= represents the end time column
i need help on two things, how i solved the issue when i have to check times like 12:00 am or 1:00 am that are already in the table like the example at the begins of the question and to check my query because is think there has to be a better solution to implement this kind of functionality
You obviously have a date somewhere in the table, as well, or you would not be able to book resources. This leads to a couple of possibilities.
- Query both time and date (can get complex, but you can make this a udf for reuse)
- Use DateTime instead of date and time columns
- if in SQL Server, consider creating a CLR function to handle this, as the CLR (.NET code) will more efficiently determine "is in time range"
I am sure there are other possibilities. The key point here is it sounds like your algorithm is failing largely due to events that spill over to another day. If that is correct, bring the day into the equation is your best bet. This can end up as a rather complex SQL statement, a change of data types (datetime instead of date and time) or creating a .NET function (CLR) to help more efficiently determine "is in range".
精彩评论