在case语句中返回消息而不是null

时间:2014-07-23 06:24:23

标签: sql sql-server tsql

我使用CASE声明在比较的基础上返回Morning,Evening或Not Yet。它效果最好但不适用于“尚未关闭”。它返回null,我不想要。我想如果没有插入截止日期,那么它应该返回'Not Yet Closed'而不是NULL。

ALTER PROCEDURE [dbo].[USP_Report_SelectComplaintsByShift] 

    @IsMorningEvening bit

AS
BEGIN

    Begin Try

      Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant,
       Case When (datepart(hour, Complaints.ClosingDateTime) < 17) then 'Morning'
       When (datepart(hour, Complaints.ClosingDateTime) >= 17) then 'Evening' 
       When ( Complaints.ClosingDateTime = '' ) then 'Not Closed Yet'
       End as ClosingShift
      from Complaints Inner Join Complainants 
      ON Complaints.Complainant_ID = Complainants.ComplainantI

END

4 个答案:

答案 0 :(得分:4)

您使用 IS NULL ELSE 部分进行检查:

试试这个

  Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant,
         Case 
            When (datepart(hour, Complaints.ClosingDateTime) < 17) Then 'Morning'
            When (datepart(hour, Complaints.ClosingDateTime) >= 17) Then 'Evening' 
            When (Complaints.ClosingDateTime IS NULL ) Then 'Not Closed Yet'
            ELSE 'Not Closed Yet'
         End as ClosingShift
  from Complaints 
  Inner Join Complainants ON Complaints.Complainant_ID = Complainants.ComplainantI 

答案 1 :(得分:2)

您必须在案例陈述中使用 Else

  Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant,
         Case 
            When (datepart(hour, Complaints.ClosingDateTime) < 17) then 'Morning'
            When (datepart(hour, Complaints.ClosingDateTime) >= 17) then 'Evening' 
            Else 'Not Closed Yet'
         End as ClosingShift
  from Complaints 
  Inner Join Complainants ON Complaints.Complainant_ID = Complainants.ComplainantI 

如果要使用NULL值检查查询,请使用以下查询:

  Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant,
         Case 
            When (datepart(hour, Complaints.ClosingDateTime) < 17) then 'Morning'
            When (datepart(hour, Complaints.ClosingDateTime) >= 17) then 'Evening' 
            When (Complaints.ClosingDateTime IS NULL) Then 'Not Closed Yet'
         End as ClosingShift
  from Complaints 
  Inner Join Complainants ON Complaints.Complainant_ID = Complainants.ComplainantI 

答案 2 :(得分:1)

试试这个:

(Complaints.ClosingDateTime = '')声明中首先使用CASE条件。

SELECT  ComplaintID, ComplaintSubject, Complainants.ComplainantName AS Complainant,
        CASE    WHEN (Complaints.ClosingDateTime IS NULL OR Complaints.ClosingDateTime = '') THEN 'Not Closed Yet'
                WHEN (DATEPART(HOUR, Complaints.ClosingDateTime) < 17) THEN 'Morning'
                WHEN (DATEPART(HOUR, Complaints.ClosingDateTime) >= 17) THEN 'Evening' 
        END AS ClosingShift
FROM    Complaints INNER JOIN
        Complainants ON Complaints.Complainant_ID = Complainants.ComplainantID

实际上,如果Complaints.ClosingDateTime = '',那么DATEPART(HOUR, Complaints.ClosingDateTime) 0 。因此(DATEPART(HOUR, Complaints.ClosingDateTime) < 17)条件已满足,您将获得结果'早上'

答案 3 :(得分:0)

请尝试使用 COALESCE 以避免null。 我想以下应该工作

  Select ComplaintID, ComplaintSubject, Complainants.ComplainantName as Complainant,
  COALESCE (( Case When (datepart(hour, Complaints.ClosingDateTime) < 17) then 'Morning'
   When (datepart(hour, Complaints.ClosingDateTime) >= 17) then 'Evening' 
   When ( Complaints.ClosingDateTime = '' ) then 'Not Closed Yet'
   End),'Not Closed Yet') as ClosingShift
  from Complaints Inner Join Complainants 
  ON Complaints.Complainant_ID = Complainants.ComplainantI