I have a general question when you use the CASE statement in SQL (Server 2008), and more than one of the WHEN conditions is true, but the resulting flag should be different.
This is a hypothetical example, but can be passed when applying checks on multiple columns to classify data in rows. The result of the code below depends on how the cases are ordered, since both values ββare true.
DECLARE @TESTSTRING varchar(5)
SET @TESTSTRING = 'hello'
SELECT CASE
WHEN @TESTSTRING = 'hello' THEN '0'
WHEN @TESTSTRING <> 'hi' THEN '1'
ELSE 'N/A'
END AS [Output]
In general, would it be bad practice to create flags this way? Would a WHERE, OR clause be better?
source
share