I am trying to get status of my tickets using case expression. It's working perfectly but not returning Not Available
. Rather than that it's returning a null value in Status as result.
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. Your WHERE
is filtering the rows or your table is empty.
Remove the WHERE
and see if it returns rows, to see if the table isn't empty. If so then make sure that your ID
filter is correct.
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
:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.