Friday, November 24, 2006

How to use Decode in Oracle SQL

The use of the decode function in Oracle SQL is pretty straightforward. You simply specify the column that you need conditional logic on and then specify the conditions as follows:

SELECT DECODE (value,
<if this value>, <return this value>,
< if this value>, <return this value>,
....)
FROM dual;


If you have a default value that you want to display if any of the conditions are not met, you can add another comma and specify the default value like this:

SELECT DECODE (value,
if this value, return this value,
if this value, return this value,
otherwise this value
FROM dual;



For a thorough description of all features of this DECODE function, see the Puget Sound Oracle User's Group description.


Here are some further examples from another post on my blog:

Most Oracle SQL users do not realize the power of this function. Hopefully this post will enlighten some on it.