sql获取两小时之间的可用记录

时间:2014-03-14 21:42:48

标签: sql sql-server-2008

我有一个包含以下值的表格,我需要显示按计划可用的名称


Name | EntryTime|ExitTime  |BreakStart |BreakEnd 

David| 06:00    | 18:00    | 06:30     | 8:00 |
Kim  | 18:00    | 06:00    | 20:00     | 21:00|
Jess | 06:30    | 15:00    | 12:00     | 13:00|
Mike | 18:00    | 06:00    | 19:00     | 20:00|

例如:

If the hours is 20:30
   the query return Kim
If the hours is 11:00
   the query return David, Jess

我有这个,但当我需要返回Kim或Mike的名字

时不能工作
SELECT Name FROM employees 
WHERE (EntryTime > @time AND ExitTime < @time)
AND (@time not between  BreakStart AND BreakEnd)

1 个答案:

答案 0 :(得分:1)

从“Kim”的例子中,你似乎并不关心休息。我认为以下是你想要的逻辑:

SELECT Name
FROM employees 
WHERE (EntryTime < ExitTime and @time between EntryTime and ExitTime) or
      (ExitTime < EntryTime and @time not between ExitTime and EntryTime);

我一般不主张在日期和时间之间使用`。在这种情况下,它使逻辑可读。