Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CASE Expression with T-SQL

I am using:

SELECT
  CASE SR.[ContainerId] WHEN SR.[ContainerId] IS NULL
    THEN 0
    ELSE 1
  END AS [IsSampleReceived]
FROM SomeTable SR where SomeCondition

Its not giving me the desired result. IsSampleReceived is always 1. I don't know why, maybe there's some thing wrong in WHEN SR.[ContainerId] IS NULL.

like image 546
Vaibhav Jain Avatar asked Jul 09 '26 01:07

Vaibhav Jain


1 Answers

There are two formats of using CASE and you are mixing them together.

The CASE expression has two formats:

The simple CASE expression compares an expression to a set of simple expressions to determine the result.

The searched CASE expression evaluates a set of Boolean expressions to determine the result.

See http://msdn.microsoft.com/en-us/library/ms181765.aspx

Instead, try:

select case 
        when SR.[ContainerId] is null
            then 0
        else 1
        end as [IsSampleReceived]
from SomeTable SR
where SomeCondition
like image 132
D'Arcy Rittich Avatar answered Jul 14 '26 04:07

D'Arcy Rittich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!