0

我正在尝试回答以下问题:

显示 2007 年 10 月中午到下午 5 点之间开始的所有约会

我尝试了以下查询:

SELECT EngagementNumber, StartDate, StartTime
FROM Engagements
WHERE StartDate <= CAST('2007-10-31' As DATE)
AND EndDate >= CAST('2007-10-01' AS DATE)
AND StartTime Between CAST('12:00:00' AS TIME) AND CAST('17:00:00' AS TIME)

但是,正在发生以下错误:

Msg 402, Level 16, State 1, Line 1 数据类型 datetime 和 time 在小于或等于运算符中不兼容。

我在 SQL Server Database 2008R2 版本上运行它,想知道是否有人能告诉我为什么会这样?

谢谢

4

3 回答 3

2
SELECT 
       EngagementNumber, 
       StartDate, 
       StartTime
FROM 
       Engagements
WHERE
      StartDate <= '2007-10-31' 
      AND EndDate >= '2007-10-01' 
      AND convert(char(8), StartTime , 108) BETWEEN '12:00:00' AND '17:00:00'

108恒定输出为 hh:mm:ss

于 2013-09-18T11:36:55.570 回答
1

试试这个方法

SELECT  EngagementNumber, StartDate, StartTime 
FROM    Engagements
WHERE   StartDate <= '2007-10-31T12:00:00.000' 
  AND   EndDate >=  '2007-10-01T17:00:00.000'

如果 startdate 和 enddate 是 datetime 数据类型,这将起作用

于 2013-09-18T11:34:07.137 回答
0

我只会使用datepart(hour)

SELECT EngagementNumber, StartDate, StartTime
FROM Engagements
WHERE StartDate <= CAST('2007-10-31' As DATE) AND
      EndDate >= CAST('2007-10-01' AS DATE) and
      datepart(hour, StartTime) >= 12 and datepart(hour, EndTime) < 17;
于 2013-09-18T11:40:46.380 回答