﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Question of the Day (QOD) / Testing Center  / Question of the Day for 24 Apr 2007 / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 08:19:20 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>very nice discussion by the ppl Mike, Hugo...:-)</description><pubDate>Tue, 27 Nov 2012 05:30:37 GMT</pubDate><dc:creator>kapil_kk</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>Case is one of more beatiful and powerfull things in SQL programming. Especially when you have several bit columns that translates to some states and then state as single parameter. (IsShipped, IsReady - for example)</description><pubDate>Fri, 05 Sep 2008 09:51:54 GMT</pubDate><dc:creator>Marcin Zawadzki</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>[quote][b]Mike C (4/23/2007)[/b][hr]Case  [/quote]Agreed.</description><pubDate>Sun, 09 Mar 2008 12:36:42 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;BOL has plenty of inaccuracies to go around (all of these - and a lot more - have been reported to Microsoft already):&lt;/P&gt;&lt;P&gt;"If either or both operands are NULL, &lt;STRONG&gt;&lt;EM&gt;&lt;U&gt;NULL&lt;/U&gt;&lt;/EM&gt;&lt;/STRONG&gt; is returned." &lt;STRONG&gt;&lt;EM&gt;Sources:&lt;/EM&gt;&lt;/STRONG&gt; &lt;A href="http://msdn2.microsoft.com/en-us/library/ms178590.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms178590.aspx&lt;/A&gt; (BOL, greater than operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms174978.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms174978.aspx&lt;/A&gt; (BOL, less than or equal to operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms179873.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms179873.aspx&lt;/A&gt; (BOL, less than operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms181567.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms181567.aspx&lt;/A&gt; (BOL, greater than or equal to operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms176020.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms176020.aspx&lt;/A&gt; (BOL, not equal to operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms190296.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms190296.aspx&lt;/A&gt; (BOL, non-standard not equal to operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms189808.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms189808.aspx&lt;/A&gt; (BOL, non-standard not less than operator)&lt;A href="http://msdn2.microsoft.com/en-us/library/ms184364.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms184364.aspx&lt;/A&gt; (BOL, non-standard not greater than operator)&lt;/P&gt;&lt;P&gt;"When you compare two NULL expressions, the result depends on the ANSI_NULLS setting: &lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If ANSI_NULLS is set to ON, the result is &lt;STRONG&gt;&lt;EM&gt;&lt;U&gt;NULL&lt;/U&gt;&lt;/EM&gt;&lt;/STRONG&gt;, following the ANSI convention that a NULL (or unknown) value is not equal to another NULL or unknown value."&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Source:&lt;/EM&gt;&lt;/STRONG&gt; &lt;A href="http://msdn2.microsoft.com/en-us/library/ms178590.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms178590.aspx&lt;/A&gt; (BOL, equals operator)&lt;/P&gt;&lt;P&gt;"If either or both of the expressions are NULL, the result is always &lt;STRONG&gt;&lt;EM&gt;&lt;U&gt;FALSE&lt;/U&gt;&lt;/EM&gt;&lt;/STRONG&gt;." Source: &lt;A href="http://msdn2.microsoft.com/en-us/library/ms181567.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms181567.aspx&lt;/A&gt; (BOL, greater than or equal to operator)&lt;/P&gt;</description><pubDate>Tue, 24 Apr 2007 08:54:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>&lt;P&gt;SELECT *FROM MyTableWHERE CASE WHEN mycompare1 IS NULL THEN 'A'     ELSE mycompare1 END = 'A'&lt;/P&gt;&lt;P&gt;Which is defined by ANSI as the expanded form of the COALESCE function:&lt;/P&gt;&lt;P&gt;SELECT *FROM MyTableWHERE COALESCE(mycompare1, 'A') = 'A'&lt;/P&gt;</description><pubDate>Tue, 24 Apr 2007 07:35:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>&lt;P&gt;Hi David,&lt;/P&gt;&lt;P&gt;I am aware that Books Online calls CASE a "function". This is incorrect, though, for the following two reasons.&lt;/P&gt;&lt;P&gt;1. Functions always take the form &lt;EM&gt;functionname&lt;/EM&gt;(&lt;EM&gt;argument&lt;/EM&gt;, ...). The CASE expression has a completely dissimilar form.&lt;/P&gt;&lt;P&gt;2. The SQL standards that &lt;STRONG&gt;define&lt;/STRONG&gt; 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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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, ...).&lt;/P&gt;</description><pubDate>Tue, 24 Apr 2007 07:23:00 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>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.</description><pubDate>Tue, 24 Apr 2007 06:35:00 GMT</pubDate><dc:creator>David Burrows</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>&lt;P&gt;Hi Thomas,&lt;/P&gt;&lt;P&gt;"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.&lt;/P&gt;&lt;P&gt;"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:&lt;/P&gt;&lt;P&gt;SELECT SomethingFROM SomeTableWHERE SomeColumn = CASE WHEN @SomeParameter = 'ALL' THEN SomeColumn ELSE @SomeParameter END;&lt;/P&gt;&lt;P&gt;Or a stored procedure for searching places in a radius that can be specified in either miles or kilometers:&lt;/P&gt;&lt;P&gt;SELECT SomethingFROM SomeTableWHERE DistanceInKM &amp;lt; CASE WHEN @unit = 'miles' THEN 1.609344 ELSE 1 END * @MaxDistance;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;SELECT SomethingFROM SomeTableWHERE CASE WHEN CustType = 'B' THEN FoundingDate ELSE BirthDate END &amp;lt; '19570424';&lt;/P&gt;&lt;P&gt;Need I go on?&lt;/P&gt;</description><pubDate>Tue, 24 Apr 2007 02:59:00 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>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?</description><pubDate>Tue, 24 Apr 2007 02:34:00 GMT</pubDate><dc:creator>hodgy</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>&lt;P&gt;Since SQL does not have a CASE statement, but does have a CASE expression, the answer depends on the interpretation of the question.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Or are we supposed to answer "no", because a CASE &lt;EM&gt;statement&lt;/EM&gt; 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".&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description><pubDate>Tue, 24 Apr 2007 01:43:00 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>Case &lt;EM&gt;expression&lt;/EM&gt; &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;</description><pubDate>Mon, 23 Apr 2007 22:35:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>Question of the Day for 24 Apr 2007</title><link>http://www.sqlservercentral.com/Forums/Topic356940-120-1.aspx</link><description>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.</description><pubDate>Mon, 09 Apr 2007 12:05:00 GMT</pubDate><dc:creator>Site Owners</dc:creator></item></channel></rss>