How can I compare time(7) in SQL Server?

I have a this select statement::

    SELECT [Course ID], [Course name] 
FROM Courses 
(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]))

it keeps giving the following errors::

  • Syntax error with sum in sql code
  • Joining a table value function to a MSSQL query
  • Convert Epoch to DateTime SQL Server
  • Adding a unique row count to a SQL 2008 “for xml path” statement?
  • Populate Ordinal column sequentially
  • SP_Who does not return DBName Column
  • The data types date and time are incompatible in the less than or equal to operator….

    Ambiguous column name ‘Course ID’

    How can I fix these errors and actually compare the column of data type time(7) ?

    One Solution collect form web for “How can I compare time(7) in SQL Server?”

    You need to convert the date to time i.e

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

    and to get rid of the ambiguous column you need to add a alias to your table as student_courses and courses both have courseId.

    Without running it the problem looks like its here in the subquery

    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]) 
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.