如何比较SQL Server中的时间(7)?

时间:2012-12-12 14:31:12

标签: sql sql-server

我有一个这样的选择语句::

    SELECT [Course ID], [Course name] 
FROM Courses 
WHERE [Course ID] NOT IN 
(SELECT [Course ID] from student_courses sc
  INNER JOIN Courses c ON sc.[course ID] = c.[course ID] 
  WHERE [student ID] = 1
  AND (c.[course start time] not BETWEEN Courses.[course start time] AND Courses.[course end time]
  OR c.[course end time] not BETWEEN Courses.[course start time] AND Courses.[course end time]))

它不断发出以下错误::

  

数据类型日期和时间在小于或等于运算符时不兼容....

     

不明确的列名'课程ID'

如何修复这些错误并实际比较数据类型列time(7)

1 个答案:

答案 0 :(得分:2)

您需要将日期转换为时间,即

CAST(<YOUR DATE> AS time(7))

要删除不明确的列,您需要在表格中添加别名,因为student_courses和课程都有courseId

如果没有运行它,问题看起来就像在子查询中一样

select **c.[Course ID]** from student_courses sc inner join Courses c on sc.[Course ID] = c.[Course ID]
    where [student ID]=@val1
    AND ((c.[Course start date] between Courses.[Course start date] and Courses.[Course end time])
    or (c.[Course end time] between Courses.[Course start time] and Courses.[Course end time]) 
    )