detect conflict time using query in vb.net?

馋奶兔 提交于 2020-01-06 08:38:13

问题


This is my query.

("Select Count(*) From table1 where [t1] <= @TimeIn And [t2] >= @TimeOut;", cn)

My problem here is I can't compare other times. For example, if I input 7:00-9:00 then it will be saved as a new time and if I then input 8:00-10:00 it also save in my database how can I solve this I want to prevent the second input time from conflicting with the first?


回答1:


There are three combinations of checks you need to perform when looking for conflicting Time Windows.

  1. Is the TimeIn within an existing entry (Time Window)
  2. Is the TimeOut within an existing entry
  3. Does the combination of TimeIn and TimeOut wholly cover an existing entry

This Sql should cover those options

select
    count(*) from table1 
where
    (
        @timeIn > [T1] AND @timeIn < [T2]
    ) -- TimeIn in existing window
    OR
    (
        @timeOut > [T1] AND @timeOut < [T2]
    ) -- TimeOut in existing window
    OR
    (
        @timeIn < [T1] AND @timeOut > [T2]
    ) -- TimeIn and TimeOut wholly covers existing window

If you get a 0 returned, there are no conflicts



来源:https://stackoverflow.com/questions/56644537/detect-conflict-time-using-query-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!