CASE语句不与日期部分一起使用

时间:2014-07-22 12:26:45

标签: sql sql-server-2008

我比较日期是否大于17:00:00然后如果>它应该返回其他早晨。比17:00:00我正在尝试CASE但是在>上给出错误<迹象,为什么?

ALTER PROCEDURE [dbo].[USP_Report_SelectComplaintsByShift] 

    @IsMorningEvening bit

AS
BEGIN

    Begin Try

      Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant
      CASE
      Complaints.ClosingDateTime
      When (Complaints.ClosingDateTime  < '17:00:00') then 'Morning'
      When (Complaints.ClosingDateTime > '17:00:00') then 'Evening'
      End as ClosingShift
      from Complaints Inner Join Complainants 
      ON Complaints.Complainant_ID = Complainants.ComplainantID

END

1 个答案:

答案 0 :(得分:1)

只需使用hour提取datepart()

  Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant
         (case When (datepart(hour, Complaints.ClosingDateTime) < 17) then 'Morning'
               else 'Evening'
          End) as ClosingShift
  from Complaints Inner Join
       Complainants 
       on Complaints.Complainant_ID = Complainants.ComplainantID;