<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: CASE Expression with Null Values in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821930#M4852773</link>
    <description>&lt;P&gt;If you want accurate results, use the searched case.  Who cares if it's more typing?  You want accurate results, don't you?&lt;/P&gt;</description>
    <pubDate>Thu, 08 Aug 2013 08:47:11 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2013-08-08T08:47:11Z</dc:date>
    <item>
      <title>CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaq-p/13821926</link>
      <description>&lt;P&gt;Why does the case expression ignore the condition I have to catch null values?&lt;/P&gt;
&lt;P&gt;With this test table:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;create&lt;/SPAN&gt; &lt;SPAN class="n"&gt;table&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;
&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;ID&lt;/SPAN&gt; &lt;SPAN class="n"&gt;integer&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt; &lt;SPAN class="n"&gt;varchar&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;5&lt;/SPAN&gt;&lt;SPAN class="p"&gt;));&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;insert&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;values&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;1&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'AAAA'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;insert&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;values&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;2&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'BBBB'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;insert&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;values&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;3&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'CCCC'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;insert&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;values&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;4&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;NULL&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;insert&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;values&lt;/SPAN&gt; &lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;5&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt; &lt;SPAN class="n"&gt;NULL&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;and this select statement:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN class="n"&gt;ID&lt;/SPAN&gt; &lt;SPAN class="p"&gt;,&lt;/SPAN&gt; 
&lt;SPAN class="k"&gt;case&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'AAAA'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'BBBB'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'CCCC'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;NULL&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'X'&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'U'&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;as&lt;/SPAN&gt; &lt;SPAN class="n"&gt;Status&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;FROM&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;I would expect to get 3 rows of status 'A' and two rows of status 'X'.  Instead I get 3 rows of status 'A' and two rows of status 'U'.&lt;/P&gt;
&lt;P&gt;Why doesn't the When Null line match the null values?&lt;/P&gt;
&lt;P&gt;This format does give my expected results:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN class="n"&gt;ID&lt;/SPAN&gt; &lt;SPAN class="p"&gt;,&lt;/SPAN&gt; 
&lt;SPAN class="k"&gt;case&lt;/SPAN&gt; 
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'AAAA'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'BBBB'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'CCCC'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt; &lt;SPAN class="n"&gt;is&lt;/SPAN&gt; &lt;SPAN class="n"&gt;NULL&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'X'&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'U'&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;as&lt;/SPAN&gt; &lt;SPAN class="n"&gt;Status&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;FROM&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;SQL Anywhere version 12.0.1 build 3436&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2013 15:38:50 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaq-p/13821926</guid>
      <dc:creator>former_SQLA_member1694868</dc:creator>
      <dc:date>2013-08-07T15:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821927#M4852770</link>
      <description>&lt;P&gt;The first SELECT statement is an example of a 'simple case' statement where AATest.Code is being compared to NULL using the equality comparison (&lt;CODE&gt;AATest.Code = NULL&lt;/CODE&gt;).&lt;/P&gt;
&lt;P&gt;The second SELECT statement is an example of a 'searched case' statement where each condition is specified individually. This allows you to specify &lt;CODE&gt;AATest.Code is NULL&lt;/CODE&gt; instead of &lt;CODE&gt;AATest.Code = NULL&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;The expression &lt;CODE&gt;AATest.Code = NULL&lt;/CODE&gt; will always evaluate to 'unknown' and never 'true', therefore the condition will never be satisfied. This is standard &lt;A href="https://en.wikipedia.org/wiki/Null_%28SQL%29#CASE_expressions"&gt;SQL behavior&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2013 16:16:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821927#M4852770</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2013-08-07T16:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821928#M4852771</link>
      <description>&lt;P&gt;Got it.  Thanks for the clarification.  When handling this situation is it more common to use the searched case (which seems like quite a bit more typing) or use the simple case with a more filtered initial comparison like this:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="n"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN class="n"&gt;ID&lt;/SPAN&gt; &lt;SPAN class="p"&gt;,&lt;/SPAN&gt; 
&lt;SPAN class="k"&gt;case&lt;/SPAN&gt; &lt;SPAN class="n"&gt;coalesce&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="o"&gt;.&lt;/SPAN&gt;&lt;SPAN class="n"&gt;Code&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt;&lt;SPAN class="s"&gt;'X'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'AAAA'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'BBBB'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'CCCC'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'A'&lt;/SPAN&gt;
    &lt;SPAN class="n"&gt;when&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'X'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'X'&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'U'&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;as&lt;/SPAN&gt; &lt;SPAN class="n"&gt;Status&lt;/SPAN&gt;
&lt;SPAN class="n"&gt;FROM&lt;/SPAN&gt; &lt;SPAN class="n"&gt;AATest&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 07 Aug 2013 16:39:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821928#M4852771</guid>
      <dc:creator>former_SQLA_member1694868</dc:creator>
      <dc:date>2013-08-07T16:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821929#M4852772</link>
      <description>&lt;P&gt;Note the different behaviour when using an IF expression, as documented &lt;A href="http://sqlanywhere-forum.sap.com/questions/1739#17842"&gt;here&lt;/A&gt;...&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2013 03:58:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821929#M4852772</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2013-08-08T03:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821930#M4852773</link>
      <description>&lt;P&gt;If you want accurate results, use the searched case.  Who cares if it's more typing?  You want accurate results, don't you?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2013 08:47:11 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821930#M4852773</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2013-08-08T08:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821931#M4852774</link>
      <description>&lt;P&gt;Hm, besides the particular NULL comparison effect, I'd usually prefer the simple form if it's exactly "that simple case": i.e. comparing one expression to a set of constant values... - here, using the simple form is more comprehensible IMHO.&lt;/P&gt;
&lt;P&gt;So I would not generally recommend the "searched case" - and unless one is aware of the "= NULL isn't IS NULL" problem, the searched case will tend to be a potential pitfall, as well...&lt;/P&gt;
&lt;P&gt;NULLs are difficult, but usable:)&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2013 09:23:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821931#M4852774</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2013-08-08T09:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: CASE Expression with Null Values</title>
      <link>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821932#M4852775</link>
      <description>&lt;P&gt;I agree: code the simple case if you can get away with it (you can't, here).&lt;/P&gt;
&lt;P&gt;Re: "NULLs are difficult" - if "difficult" is German for "despicable" then I agree with that too &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2013 16:31:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-expression-with-null-values/qaa-p/13821932#M4852775</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2013-08-08T16:31:01Z</dc:date>
    </item>
  </channel>
</rss>

