ArticlesBase.com - Free Articles Directory
Free Online Articles Directory
13.10.2008 Sign In Register Hello Guest
Email:
Password:
Remember Me 
forgot your password?


Decode Function

Author: sailaja Author Ranking Blue | Posted: 06-06-2008 | Comments: 0 | Views: 25 | Rating:  (52) Article Popularity - Blue (?) Got a Question? Ask.
Sign Up Now!

DECODE Function

 

DECODE is a very useful function in Oracle queries. And it has the functionality of an IF-THEN-ELSE logic. It is used to display different things based on different values in a column.

The following are the formats for the DECODE function:

DECODE (value, search1, result1 [, search2, result2,] [, search3, result3,]…….[, default])

                                                            OR

DECODE (value, if1, then1 [, if2, then2,]  [, if3, then3,]……..[, else])

 

                                                            OR

 

DECODE (value, find1, replace1 [, find2, replace2,]  [find3, replace3,]……[, default])

 

In the above formats:

value represents is either static value, expression or column in a table / view

search1, search2, search3, …… or if1, if2, if3, …… or find1, find2, find3, …….

is the value that is compared against the value that in the first argument of DECODE.

result1, result2, result3, …. or then1, then2, then3,…. or replace1, replace2, replace3, ……..

is the value returned, if value is equal to search1, search2, search3, ….. or if1, if2, if3,…… or find1, find2, find3,………. respectively.

default or else is optional.  If no matches are found, the DECODE will return the default value.  If default is omitted, then the DECODE function will return NULL.

So, DECODE compares value to each search value one by one. If value is equal to a search, then Oracle Database returns the corresponding result. If no match is found, then Oracle returns default value.  If default is omitted, then Oracle returns NULL.

 

The following are the examples:

 

Example1:

This example decodes the value deptno. If deptno is 10, then the function returns 'Sales'; if deptno is 20, then it returns 'Production'; and so forth. If deptno is not 10, 20 or 30 then the function returns 'NULL'.

SELECT ename, sal,

       DECODE (deptno, 10, 'Sales',

                       20, 'Production',

                       30, 'Research') AS Department    FROM emp;

 

Example2: 

This example decodes the value deptno. If deptno is 10, then the function returns 'Sales'; if deptno is 20, then it returns 'Production'; and so forth. If deptno is not 10, 20 or 30 then the function returns 'Accounting'.

SELECT ename, sal,

       DECODE (deptno, 10, 'Sales',

                       20, 'Production',

                       30, 'Research', ‘Accounting’) AS Department FROM emp;


Example3: 

This example decodes the value deptno. If deptno is 10, then the function returns 'Sales'; if deptno is 20, then it returns 'Production'; and so forth. If deptno is not 10, 20 or 30 then the function returns deptno value.

SELECT ename, sal,

       DECODE (deptno, 10, 'Sales',

                       20, 'Production',

                       30, 'Research', deptno)

  FROM emp;


Example4: 

This example returns the biggest of a given two numbers. If SIGN(&&a-&&b) is 1, then the function returns the value of a; if SIGN(&&a-&&b)is -1, then it returns the value of b. If SIGN(&&a-&&b)is not 1, -1 then the function returns ‘Both are equal’value.

 

This example returns the biggest of a give two numbers.  If

SELECT DECODE (SIGN(&&a-&&b),

               1, ‘Big is ‘||&a, -1, 'Big is '||&b,

               ‘Both are same')

  FROM dual;

Example5:

This example returns numbers in words (for 1 to 10). If value is 1, then the function returns the value of One; if value is 2, then it returns the value of Two; if value is 3, then it returns the value of Three; and so forth. if value is not 1, 2, 3, 4, 5,……10 then the function returns ‘Sorry, I do not know’value.

SELECT DECODE (&no,1,’One’,2,’Two’,3,’Three’,4,’Four’

               5,’Five’,6,’Six’,7,’Seven’,8,’Eight’,9,’Nine’,10,’Ten’,’Sorry, I do not know’)

FROM dual;

Example6:

This example decodes the value SIGN(sal-2000). If SIGN(sal-2000) is 1, then the function returns 'Salary more than 2000'; if SIGN(sal-2000) is -1, then it returns 'Salary less than 2000'. If SIGN(sal-2000)is not 1, -1 then the function returns ‘Salary is 2000’ value.

SELECT ename, job, sal,

       DECODE (SIGN(sal-2000),

       1, ‘Salary more than 2000’,

      -1, ‘Salary less than 2000’,

       ‘Salary is 2000') “Status”

  FROM emp;

 

Example7: 

This example decodes the value SIGN(sal-2000). If SIGN(sal-2000) is 1, then the function returns 'Salary more than 2000'; if SIGN(sal-2000) is -1, then it returns 'Salary less than 2000'. If SIGN(sal-2000)is not 1, -1 then the function returns ‘Salary is 2000’ value.

SELECT ename, job, sal, DECODE (SIGN(sal-2000),

                        1, ‘Salary more than 2000’,

                       -1, ‘Salary less than 2000’,

                       ‘Salary is 2000')

  FROM emp;

Example8:

This example decodes the value of job. If job is ‘CLERK’, then the function returns corresponding ename value. If job is not ‘CLERK’ then the function returns NULL value.

SELECT ename, deptno, sal,

       DECODE (LTRIM(RTRIM(UPPER(job))),

       ‘CLERK’, ename) “Clerks”

  FROM emp;

 

Example9:

This example decodes the value of SIGN ((SELECT MAX(sal) FROM emp)-sal).  If SIGN((SELECT MAX(sal) FROM emp)-sal) is 1, then the function returns corresponding ‘More than Max Salary’ value; If SIGN((SELECT MAX(sal) FROM emp)-sal) is -1, then the function returns corresponding ‘Less than Max Salary’ value; If SIGN((SELECT MAX(sal) FROM emp)-sal) is not 1 or -1 then the function returns ‘Maximum Salary’ value.

SELECT ename, sal,

DECODE (SIGN((SELECT MAX(sal) FROM emp)-sal),

        1, ‘More than Max Salary’

       -1, ‘Less than Max Salary’

         , ‘Maximum Salary’) “Status” FROM emp;

 

Rate this Article: Current: 4.3 / 5 stars - 3 vote(s).

Article Tags: Decode

Article Source: http://www.articlesbase.com/databases-articles/decode-function-441103.html

Print this Article Print article   Email to a Friend Send to friend   Publish this Article on your Website Publish this Article   Send Author Feedback Author feedback  
About the Author:

Sailaja Pasupuleti

Submitting articles has become one of the most popular means of generating quality backlinks and targeted traffic to your website. Join us today - It's Free!

Article Comments

Comment on this article Comment on this article
Your Name
Your Email:
Comment Body
Enter Validation Code: Captcha


Got a Question? Ask.

Ask the community a question about this article:

Frequently Asked Questions

Explain defect life cycle
By: testing | 21-04-2008
explain defect life cycle

Can I retieve an old AIM convosation?
By: mimipres05 | 17-04-2008
Can I retrieve and old AIM convosation that i already deleted?

Tricia Walsh-Smith on YouTube
By: womannotagirl | 16-04-2008
Is Tricia Walsh-Smith crazy?  Or is she saying what so many women would like to say about their ex- husbands or boyfriends?

How do i import my aol mail contacts to ms outlook ...
By: jhny | 14-04-2008
how do i import my aol mail contacts to ms outlook adress book

Riddle
By: CyndiDrow | 13-04-2008
How did the car owner feel when his car ran over him?

Cracking and pushing on foundation due to roots
By: rfrompitt | 06-04-2008
This winter I started to see cracking and pushing in our foundation could an oak tree 25ft. away cause this? The tree and house are 30 years old and the cracks only appeared this winter. The ground outside also seems to have sunk about 3 inches.

Q&A Powered by:
Powered by Yedda 

Latest Databases Articles

Learning Microsoft Excel Picture Book (show Me Don't Tell Me)
By: Raland Brunson | 10/10/2008
The easiest and quickest way to learn anything is with pictures and illustrations. "Learning Microsoft Excel with Pictures" is a instruction manual that is filled with illustrations on how and where and what to do."

Ms Access Databases Don't Have to Look Unprofessional by Nicholas Brown
By: Nicholas Brown | 30/09/2008
Tips and tricks to help maximize the usability of your database by improving the user interface.

Get Rid of Porn-how to Get Rid of Porn From Your Computer
By: Valeri Tkatchenko | 29/09/2008
Get rid of porn today! This is important to get rid of porn from your hard drive or it can lend you in some serious trouble! Sometimes while you browse the internet porn fragments and popups install themselves into your registry and it is almost impossible to get rid of this stuff. They always popup on your screen and do generally have viruses and spy ware attached to them! Whether you downloaded the porn yourself or it entered the computer by itself you need to get rid of porn fast!!!

Using Your it to Excel in Tough Times
By: Robin George | 24/09/2008
Find out how to beat the current difficult financial climate, using the software on your computer.

Using Linq to Objects in C#
By: LinqHelp | 24/09/2008
In this tutorial, we will be looking at using LINQ to Objects. We will be creating a Windows Forms Application that will first define an array of numbers, and then we will use LINQ to Objects to interact with this collection of numbers.

Pulling Data From Multiple Tables Into a Single Query in Ms Access by Nicholas Brown
By: Nicholas Brown | 17/09/2008
A brief how-to on linking multiple tables within a single query. With this tool, users can develop more complex queries, reducing the need for several queries. This will also reduce the size of the database making it more efficient and easier to navigate.

Creating Linq to Sql Entities in Vb.net
By: LinqHelp | 15/09/2008
This tutorial will show how we can bypass the Designer and write the class ourselves, so that we get a better understanding of what's going on. For this example, we will be using a SQL database with one table and three columns - id, name, and city.

Creating Linq to Sql Entities in C#
By: LinqHelp | 15/09/2008
This tutorial will show how we can bypass the Designer and write the class ourselves, so that we get a better understanding of what's going on. For this example, we will be using a SQL database with one table and three columns - id, name, and city.

Article Categories






Give Feedback

Sign up for our email newsletter

Receive updates, enter your email below