Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Question of the Day for 24 Apr 2007 Expand / Collapse
Author
Message
Posted Monday, April 9, 2007 12:05 PM
SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, May 18, 2007 3:36 PM
Points: 10,039, 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.
Post #356940
Posted Monday, April 23, 2007 10:35 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135
Case expression
Post #360440
Posted Tuesday, April 24, 2007 1:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:10 PM
Points: 6,046, Visits: 8,325

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
Post #360455
Posted Tuesday, April 24, 2007 2:34 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 4:39 AM
Points: 1,865, Visits: 593
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
Post #360461
Posted Tuesday, April 24, 2007 2:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:10 PM
Points: 6,046, Visits: 8,325

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
Post #360465
Posted Tuesday, April 24, 2007 6:35 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:55 AM
Points: 6,954, Visits: 7,076
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.

Post #360536
Posted Tuesday, April 24, 2007 7:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:10 PM
Points: 6,046, Visits: 8,325

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
Post #360554
Posted Tuesday, April 24, 2007 7:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135

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'

Post #360560
Posted Tuesday, April 24, 2007 8:54 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 1:20 PM
Points: 1,276, Visits: 1,135

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)

Post #360591
Posted Sunday, March 9, 2008 12:36 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 9:14 AM
Points: 9,294, Visits: 9,483
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."
Post #466413
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse