<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>kwatog &#38; co.&#187; when</title>
	<atom:link href="http://kwatog.com/tag/when/feed/" rel="self" type="application/rss+xml" />
	<link>http://kwatog.com</link>
	<description>tech notes and general nonsense</description>
	<lastBuildDate>Tue, 31 Jan 2012 06:27:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Oracle CASE WHEN Statement</title>
		<link>http://kwatog.com/blog/oracle/oracle-case-when-statement/</link>
		<comments>http://kwatog.com/blog/oracle/oracle-case-when-statement/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:02:14 +0000</pubDate>
		<dc:creator>kwatog</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[10g]]></category>
		<category><![CDATA[11g]]></category>
		<category><![CDATA[9i]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[when]]></category>
		<guid isPermaLink="false">http://kwatog.com/?p=182</guid>
		<description><![CDATA[Another oracle sql statement that you may need in order to simplify and optimize your code is the use of CASE statement. Here&#8217;s how it is being used. Syntax #1 CASE [ expression ] WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 ... WHEN condition_n THEN result_n ELSE result END Example SELECT last_name, commission_pct, (CASE [...]]]></description>
			<content:encoded><![CDATA[<p>Another oracle sql statement that you may need in order to simplify and optimize your code is the use of CASE statement. Here&#8217;s how it is being used. </p>
<h3>Syntax #1</h3>
<p><code><br />
CASE  [ expression ]<br />
  WHEN condition_1 THEN result_1<br />
  WHEN condition_2 THEN result_2<br />
  ...<br />
  WHEN condition_n THEN result_n<br />
  ELSE result<br />
END<br />
</code></p>
<h3>Example</h3>
<p><code><br />
SELECT last_name, commission_pct,<br />
  (CASE commission_pct<br />
    WHEN 0.1 THEN ‘Low’<br />
    WHEN 0.15 THEN ‘Average’<br />
    WHEN 0.2 THEN ‘High’<br />
    ELSE ‘N/A’<br />
  END ) Commission<br />
FROM employees ORDER BY last_name;<br />
</code></p>
<h3>Syntax #2</h3>
<p><code><br />
CASE<br />
  WHEN [ condition_1 ] THEN result_1<br />
  WHEN [ condition_2 ] THEN result_2<br />
  ...<br />
  WHEN [ condition_n ] THEN result_n<br />
  ELSE result<br />
END<br />
</code></p>
<h3>Example</h3>
<p><code><br />
    SELECT m.paper_type    ,<br />
       m.paper_brand   ,<br />
       m.paper_grammage,<br />
       m.paper_width   ,<br />
       m.paper_length  ,<br />
       m.paper_stock_no,<br />
       (<br />
       CASE<br />
       WHEN NVL(smc.qty_reserved_onhand,0)  != NVL(st.sum_qty_r_oh,0) THEN 'QROH NOT TALLY'<br />
       WHEN NVL(smc.qty_reserved_incom,0)   != NVL(st.sum_qty_r_incom,0) THEN 'QRINC NOT TALLY'<br />
       WHEN NVL(smc.ton_reserved_onhand,0)  != NVL(st.sum_ton_r_oh,0) THEN 'TROH NOT TALLY'<br />
       WHEN NVL(smc.ton_reserved_incom,0)   != NVL(st.sum_ton_r_incom,0) THEN 'TRINC NOT TALLY'<br />
       END<br />
       ) ERROR_MESSAGE<br />
from  paper_stock_master m<br />
</code></p>
<p>
Personally, I use Syntax #2 most of the time. I don&#8217;t know why but maybe it just happened that I find it more suitable to use in the situations that I needed the CASE statement.
</p>
<h3>Difference Between DECODE and CASE WHEN</h3>
<p>The most fundamental difference between the two oracle statements is that in DECODE, you are comparing a field or value in a given set of values or fields. It&#8217;s actually a shorthand for IF..ELSIF..ELSE statement with the condition all set to equals (=). CASE WHEN is almost the same but you can use any conditional operator (i.e, =,!=, >, >= , <, <=, etc) thereby giving you more freedom and flexibility in writing your code.
</p>
</p>
<p>In may instances CASE WHEN helped me reduce the number of lines of my codes as well as optimize the performance. On top of that, I no longer need a full pl/sql program with multiple queries and even cursors to be able to provide the result that a simple CASE statement can provide. However CASE, like any other sql commands, is only applicable for certain scenarios. It&#8217;s not a magic keyword that will solve/apply to everything. So you have to weigh your options if you need it or not.
</p>
<p>Ciao!</p>
<h4>Incoming search terms:</h4><ul><li>oracle case when</li><li>oracle case sta</li><li>sql optimize case statement</li><li>performance select case statement in oracle</li><li>oracle case when syntax</li><li>oracle case when statement</li><li>oracle case when as</li><li>case statement in oracle</li><li>oracle case decode optimization</li><li>optimize the case statement in orac;e</li></ul>]]></content:encoded>
			<wfw:commentRss>http://kwatog.com/blog/oracle/oracle-case-when-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!--
Hyper cache file: 89f9963b463b93f9e86b66a939ecbd3d
Cache created: 05-02-2012 02:31:16
HCE Version: 0.9.8
Load AVG: 0.18(5)
-->
