This Oracle tutorial explains how to use the Oracle/PLSQL DECODE function with syntax and examples.
The Oracle/PLSQL DECODE function has the functionality of an IF-THEN-ELSE statement.
The syntax for the DECODE function in Oracle/PLSQL is:
The value to compare. It is automatically converted to the data type of the first search value before comparing.
The value that is compared against expression. All search values are automatically converted to the data type of the first search value before comparing.
The value returned, if expression is equal to search.
Optional. If no matches are found, the DECODE function will return default. If default is omitted, then the DECODE function will return NULL (if no matches are found).
The DECODE function returns a value that is the same datatype as the first result in the list. If the first result is NULL, then the return value is converted to VARCHAR2. If the first result has a datatype of CHAR, then the return value is converted to VARCHAR2. If no matches are found, the default value is returned. If default is omitted and no matches are found, then NULL is returned.
The DECODE function can be used in the following versions of Oracle/PLSQL:
The DECODE function can be used in Oracle/PLSQL.
You could use the DECODE function in a SQL statement as follows:
The above DECODE statement is equivalent to the following IF-THEN-ELSE statement:
The DECODE function will compare each supplier_id value, one by one.
Question: One of our viewers wanted to know how to use the DECODE function to compare two dates (ie: date1 and date2), where if date1 > date2, the DECODE function should return date2. Otherwise, the DECODE function should return date1.
Answer: To accomplish this, use the DECODE function as follows:
The formula below would equal 0, if date1 is greater than date2:
Helpful Tip #1: One of our viewers suggested combining the SIGN function with the DECODE function as follows:
The date example above could be modified as follows:
The SIGN/DECODE combination is also helpful for numeric comparisons e.g. Sales Bonuses
Helpful Tip #2: One of our viewers suggested using the LEAST function (instead of the DECODE function) as follows:
The date example above could be modified as follows:
Question: I would like to know if it's possible to use the DECODE function for ranges of numbers, ie 1-10 = 'category 1', 11-20 = 'category 2', rather than having to individually decode each number.
Answer: Unfortunately, you can not use the DECODE function for ranges of numbers. However, you can try to create a formula that will evaluate to one number for a given range, and another number for the next range, and so on.
For example:
In this example, based on the formula:
The formula will evaluate to 0, if the supplier_id is between 1 and 10. The formula will evaluate to 1, if the supplier_id is between 11 and 20. The formula will evaluate to 2, if the supplier_id is between 21 and 30.
and so on...
Question: I need to write a DECODE statement that will return the following:
If yrs_of_service < 1 then return 0.04 If yrs_of_service >= 1 and < 5 then return 0.04 If yrs_of_service > 5 then return 0.06
How can I do this?
Answer: You will need to create a formula that will evaluate to a single number for each one of your ranges.
For example:
Question: Is there a limit to the number of arguments that you can have in one DECODE statement? I'm getting an error, "ORA-00939: too many arguments for function".
Answer: Yes, the maximum number of components that you can have in a DECODE function is 255. This includes the expression, search, and result arguments.