Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Question of the Day for 24 Apr 2007


Question of the Day for 24 Apr 2007

Author
Message
Site Owners
Site Owners
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10081 Visits: 1
Comments posted to this topic are about the Question of the Day for 24 Apr 2007 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=1018.
Mike C
Mike C
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1168
Case expression
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11545

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.




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
hodgy
hodgy
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1869 Visits: 596
Assuming the author does mean a CASE expression, and then the answer is "yes", you would have to wonder why you would use it at all, as (in the example given in the question):
select *
from MyTable
where mycompare1 = 'A'
is surely much simply to write and understand.

Can anyone give an example of where a CASE expression should/could be used in a WHERE clause?

Life: it twists and turns like a twisty turny thing

Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11545

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 Something
FROM SomeTable
WHERE 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 Something
FROM SomeTable
WHERE 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 Something
FROM SomeTable
WHERE CASE WHEN CustType = 'B' THEN FoundingDate ELSE BirthDate END < '19570424';

Need I go on?




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7961 Visits: 9404
CASE is neither a 'statement' nor an 'expression' it is a function, it just happens to take expressions for input and produces an expression for output.


Far away is close at hand in the images of elsewhere.

Anon.


Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11545

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, ...).




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Mike C
Mike C
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1168

SELECT *
FROM MyTable
WHERE 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 MyTable
WHERE COALESCE(mycompare1, 'A') = 'A'


Mike C
Mike C
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1349 Visits: 1168

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:

  • If ANSI_NULLS is set to ON, the result is NULL, following the ANSI convention that a NULL (or unknown) value is not equal to another NULL or unknown value."

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)


RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9436 Visits: 9517
Mike C (4/23/2007)
Case


Agreed.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search