<?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: if/else vs case in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaa-p/13836323#M4867166</link>
    <description>&lt;P&gt;Just to add one big difference between IF and CASE expressions with ELSE clauses I came about based on &lt;A href="http://sqlanywhere-forum.sap.com/questions/17837/case-expression-with-null-values"&gt;Siger's FAQ on CASE exporessions with NULL values&lt;/A&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;With IF expressions, if the condition is UNKNOWN, the IF expression returns NULL - it does not return the value from the ELSE clause (which has been a surprise to me now and then...).&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;With CASE expressions, if expression0 is NULL, then the value from the ELSE clause will be returned.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;A simple repro:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="k"&gt;begin&lt;/SPAN&gt;
   &lt;SPAN class="k"&gt;declare&lt;/SPAN&gt; &lt;SPAN class="n"&gt;n&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;int&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="k"&gt;null&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="k"&gt;select&lt;/SPAN&gt;
      &lt;SPAN class="n"&gt;if&lt;/SPAN&gt; &lt;SPAN class="n"&gt;n&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'true'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'false'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;end&lt;/SPAN&gt; &lt;SPAN class="n"&gt;if&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;n&lt;/SPAN&gt; &lt;SPAN class="k"&gt;when&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'true'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'false'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt;
      &lt;SPAN class="k"&gt;case&lt;/SPAN&gt; &lt;SPAN class="k"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;n&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'true'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'false'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;end&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;returns NULL, 'false', 'false'&lt;/P&gt;</description>
    <pubDate>Thu, 08 Aug 2013 03:56:29 GMT</pubDate>
    <dc:creator>VolkerBarth</dc:creator>
    <dc:date>2013-08-08T03:56:29Z</dc:date>
    <item>
      <title>if/else vs case</title>
      <link>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaq-p/13836321</link>
      <description>&lt;P&gt;Wondering if anyone has an opinion on the following.  I have query where I need to eliminate the data in a field under a certain condition:  if the quantity field of a record is 0 then I don't need to see the location field of that same record.  &lt;/P&gt;

&lt;P&gt;I've set up a case statement as such: &lt;/P&gt;

&lt;P&gt;case when quantity = 0 then NULL when quantity &amp;gt; 0 then [Location]
end) as [Location]&lt;/P&gt;

&lt;P&gt;Then I saw if/else and set this up: &lt;/P&gt;

&lt;P&gt;if quantity &amp;gt; 0 then [Location] else NULL
endif as [Location]&lt;/P&gt;

&lt;P&gt;They appear to pull and display the same data.  Is there a benefit to either? &lt;/P&gt;

&lt;P&gt;Thank you. &lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2011 16:22:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaq-p/13836321</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-03-01T16:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: if/else vs case</title>
      <link>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaa-p/13836322#M4867165</link>
      <description>&lt;P&gt;In general, I would prefer&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;if-expressions&lt;/STRONG&gt; for typical boolean comparisons and &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;case-expressions&lt;/STRONG&gt; for more than two cases.&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;That's comparable to the question whether to use &lt;STRONG&gt;if&lt;/STRONG&gt; or &lt;STRONG&gt;switch/case&lt;/STRONG&gt; in C/C++ and similar programming languages.&lt;/P&gt;

&lt;P&gt;So, for your sample, I would suggest to use the if-expression.&lt;/P&gt;

&lt;P&gt;Note that there's another variant of the case-expression (though not possible in your sample):&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;CASE expression0
WHEN expression1 THEN expression2, ...
WHEN expression3 THEN expression4, ...
                 ELSE expressionX
END CASE
&lt;/LI-CODE&gt;

&lt;P&gt;This does test expression0 against a list of expressions (expression1, expression3, ...) and is somewhat shorther than the &lt;EM&gt;CASE WHEN search-condition ...&lt;/EM&gt; syntax.&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Just to add:&lt;/P&gt;

&lt;P&gt;When the expression in the ELSE clause (both with IF and CASE) should return NULL (as in your example), &lt;STRONG&gt;then you can leave out the ELSE clause&lt;/STRONG&gt; - omiting it is identical to &lt;STRONG&gt;ELSE NULL&lt;/STRONG&gt;. So your expression could be reduced to:&lt;/P&gt;

&lt;LI-CODE lang="sql"&gt;if quantity &amp;gt; 0 then [Location] endif as [Location]
&lt;/LI-CODE&gt;

&lt;P&gt;As always, leaving out the ELSE part may decrease the readibility based on whether one is used to the default NULL result or not.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2011 17:30:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaa-p/13836322#M4867165</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2011-03-01T17:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: if/else vs case</title>
      <link>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaa-p/13836323#M4867166</link>
      <description>&lt;P&gt;Just to add one big difference between IF and CASE expressions with ELSE clauses I came about based on &lt;A href="http://sqlanywhere-forum.sap.com/questions/17837/case-expression-with-null-values"&gt;Siger's FAQ on CASE exporessions with NULL values&lt;/A&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;With IF expressions, if the condition is UNKNOWN, the IF expression returns NULL - it does not return the value from the ELSE clause (which has been a surprise to me now and then...).&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;With CASE expressions, if expression0 is NULL, then the value from the ELSE clause will be returned.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;A simple repro:&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="k"&gt;begin&lt;/SPAN&gt;
   &lt;SPAN class="k"&gt;declare&lt;/SPAN&gt; &lt;SPAN class="n"&gt;n&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;int&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="k"&gt;null&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="k"&gt;select&lt;/SPAN&gt;
      &lt;SPAN class="n"&gt;if&lt;/SPAN&gt; &lt;SPAN class="n"&gt;n&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'true'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'false'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;end&lt;/SPAN&gt; &lt;SPAN class="n"&gt;if&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;n&lt;/SPAN&gt; &lt;SPAN class="k"&gt;when&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'true'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'false'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;&lt;SPAN class="p"&gt;,&lt;/SPAN&gt;
      &lt;SPAN class="k"&gt;case&lt;/SPAN&gt; &lt;SPAN class="k"&gt;when&lt;/SPAN&gt; &lt;SPAN class="n"&gt;n&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="mi"&gt;1&lt;/SPAN&gt; &lt;SPAN class="k"&gt;then&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'true'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;else&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'false'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;end&lt;/SPAN&gt; &lt;SPAN class="k"&gt;case&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;end&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;returns NULL, 'false', 'false'&lt;/P&gt;</description>
      <pubDate>Thu, 08 Aug 2013 03:56:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/if-else-vs-case/qaa-p/13836323#M4867166</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2013-08-08T03:56:29Z</dc:date>
    </item>
  </channel>
</rss>

