简体   繁体   中英

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

so 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.

  1. Query both time and date (can get complex, but you can make this a udf for reuse)
  2. Use DateTime instead of date and time columns
  3. 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM