简体   繁体   English

CASE表达式返回NULL值

[英]CASE expression returning NULL value

I am trying to get status of my tickets using case expression. 我正在尝试使用案例表达式来获取我的票证状态。 It's working perfectly but not returning Not Available . 它工作正常,但未返回Not Available Rather than that it's returning a null value in Status as result. 而不是返回Status中的空值作为结果。

What's wrong with this query? 这个查询怎么了?

Secondly can I make further if else on returning of operations? 其次,如果还需要其他操作,我可以做得更多吗?

Select 
Case 
      When Status=1 and isTaken=1 then 'Open'
      When Status=4 and isTaken=0 then 'Expire'
      When Status=2 and isTaken=0 then 'Pending'
      When Status=0 and isTaken=0 then 'Close'

Else 'Not Available' End As 'Status' 
from Tickets 
Where Id='1234567890'

Check Image for more explanation. 检查图像以获取更多说明。

在此处输入图片说明

Your query is not returning NULL , it's returning 0 rows. 您的查询未返回NULL ,而是返回0行。 Your WHERE is filtering the rows or your table is empty. 您的WHERE正在过滤行或表为空。

Remove the WHERE and see if it returns rows, to see if the table isn't empty. 删除WHERE并查看它是否返回行,以查看表是否为空。 If so then make sure that your ID filter is correct. 如果是这样,请确保您的ID过滤器正确。

If your want to return a row even if the filter doesn't find anything then you need a NOT EXISTS or a LEFT JOIN : 如果即使过滤器没有找到任何内容,您也想返回一行,则需要NOT EXISTSLEFT JOIN

DECLARE @ID VARCHAR(20) = '1234567890'

SELECT
    F.ID,
    Case 
        When Status=1 and isTaken=1 then 'Open'
        When Status=4 and isTaken=0 then 'Expire'
        When Status=2 and isTaken=0 then 'Pending'
        When Status=0 and isTaken=0 then 'Close'
    Else 'Not Available' End As 'Status' 
FROM
    (VALUES (@ID)) AS F(ID)
    LEFT JOIN Tickets AS C ON F.ID = C.Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM