Since SQL does not have a CASE statement, but does have a CASE expression, the answer depends on the interpretation of the question.
Are we supposed to assume that the question's author is one of the many people that don't know the difference between statements and expression? In that case, the answer is of course "yes" - but I really don't see why such a simple question would be worth 2 points.
Or are we supposed to answer "no", because a CASE statement doesn't exist in SQL at all, and hence can't be used anywhere, including a WHERE clause? In that case, I think the category should have been "nitpicking" instead of "T-SQL".
Since both interpretations of the question are equally valid (or rather: equallly invalid) and the points and category don't help to indicate which interpretation is the most likely, I've decided to skip this question. If the wording of the question gets changed, I'll revisit it.
Hi Thomas,
"the example given in the question" - Huh? I just went back and checked the question to make sure, but there is no example at all given in the question.
"Can anyone give an example of where a CASE expression should/could be used in a WHERE clause?" - Lots, if I have to. A very common one is that of a stored procedure with an optional search argument:
SELECT SomethingFROM SomeTableWHERE SomeColumn = CASE WHEN @SomeParameter = 'ALL' THEN SomeColumn ELSE @SomeParameter END;
Or a stored procedure for searching places in a radius that can be specified in either miles or kilometers:
SELECT SomethingFROM SomeTableWHERE DistanceInKM < CASE WHEN @unit = 'miles' THEN 1.609344 ELSE 1 END * @MaxDistance;
Or a table that uses the column FoundingDate for business customers and BirthDate for people customers in a query that reports customers older than 50 years:
SELECT SomethingFROM SomeTableWHERE CASE WHEN CustType = 'B' THEN FoundingDate ELSE BirthDate END < '19570424';
Need I go on?
Far away is close at hand in the images of elsewhere.
Anon.
Hi David,
I am aware that Books Online calls CASE a "function". This is incorrect, though, for the following two reasons.
1. Functions always take the form functionname(argument, ...). The CASE expression has a completely dissimilar form.
2. The SQL standards that define the SQL language have defined CASE as an expression. I just doublechecked this in both the ANSI-92 and the SQL-2003 standards, and my memory didn't play any tricks on me - both versions of the standard list CASE as an expression.
The fact that CASE takes expressions for input is nothing special, as all expressions and all functions take expressions for input. Your statement that CASE produces an expression for output is incorrect; the output of a CASE expression is a scalar value, just as the output of a function.
This being said, I'll be the first to admit that the difference between expressions and functions is even more nitpicky than the difference between expressions (or functions) and statements. I don't see any real need to distinguish expressions from functions (in fact, functions are a subset of expressions), whereas there is a real difference between expression (and hence functions) on the one hand and statements on the other hand.
A statement can be used on it's own, and can not be embedded in other statements or expresions on a place where an expression is expected (examples: IF, GRANT, SELECT, UPDATE, ...). Both expressions and functions can not be used on their own, but only as part of a statement or expression, in any location where an expression is expected (examples: DATEDIFF, CASE, (SELECT ...), CURRENT_USER, ...).
SELECT *FROM MyTableWHERE CASE WHEN mycompare1 IS NULL THEN 'A' ELSE mycompare1 END = 'A'
Which is defined by ANSI as the expanded form of the COALESCE function:
SELECT *FROM MyTableWHERE COALESCE(mycompare1, 'A') = 'A'
CASE is an expression, despite BOL calling it a function. The difference between an expression and a function is much less significant than the difference between an expression (or a function) and a statement, however.
BOL has plenty of inaccuracies to go around (all of these - and a lot more - have been reported to Microsoft already):
"If either or both operands are NULL, NULL is returned." Sources: http://msdn2.microsoft.com/en-us/library/ms178590.aspx (BOL, greater than operator)http://msdn2.microsoft.com/en-us/library/ms174978.aspx (BOL, less than or equal to operator)http://msdn2.microsoft.com/en-us/library/ms179873.aspx (BOL, less than operator)http://msdn2.microsoft.com/en-us/library/ms181567.aspx (BOL, greater than or equal to operator)http://msdn2.microsoft.com/en-us/library/ms176020.aspx (BOL, not equal to operator)http://msdn2.microsoft.com/en-us/library/ms190296.aspx (BOL, non-standard not equal to operator)http://msdn2.microsoft.com/en-us/library/ms189808.aspx (BOL, non-standard not less than operator)http://msdn2.microsoft.com/en-us/library/ms184364.aspx (BOL, non-standard not greater than operator)
"When you compare two NULL expressions, the result depends on the ANSI_NULLS setting:
Source: http://msdn2.microsoft.com/en-us/library/ms178590.aspx (BOL, equals operator)
"If either or both of the expressions are NULL, the result is always FALSE." Source: http://msdn2.microsoft.com/en-us/library/ms181567.aspx (BOL, greater than or equal to operator)