Purpose
DECODE() compares the condition value to each search value one by one, and return the comparison result.
If one or multiple
searchvalues are equal to theconditionvalue, this function returns theresultvalue following the firstsearchvalue that is equal to theconditionvalue.If no
searchvalue is equal to theconditionvalue, this function returns thedefaultvalue. If thedefaultvalue is omitted, this function returnsnull.
Syntax
DECODE (condition, search, result [, search, result ...][, default])
Parameters
The condition, search, result, and default parameters can be set to a value or an expression of a numeric data type (NUMBER, FLOAT, BINARY_FLOAT, or BINARY_DOUBLE) or a character data type (CHAR, VARCHAR2, NCHAR, or NVARCHAR2).
Notice
If the first result value is of a numeric data type, the returned result or default value must also be of a numeric data type. Otherwise, an error is reported.
Return type
If the
resultvalue following the firstsearchvalue that is equal to theconditionvalue is of theCHAR,VARCHAR2,NCHAR, orNVARCHAR2type, this function returns a value of theVARCHAR2type.If the
resultvalue following the firstsearchvalue that is equal to theconditionvalue is of a numeric type, this function returns aresultvalue of a numeric type.If no
searchvalue is equal to theconditionvalue:If the
defaultvalue is omitted, this function returnsnull.When the
defaultvalue is of theCHAR,VARCHAR2,NCHAR, orNVARCHAR2type, this function returns a value of theVARCHAR2type.
Examples
The following example uses the
DECODEfunction to compare the values of5 × 3 - 2and3 × 4 - 1.obclient> SELECT DECODE(SIGN((5*3-2)-(3*4-1)),0,'Equal',1,'(5 × 3 - 2) is greater','(3 × 4 - 1) is greater') FROM DUAL; +----------------------------------------------------------------------+ | DECODE(SIGN((5*3-2)-(3*4-1)),0,'Equal',1,'(5 × 3 - 2) is greater','(3 × 4 - 1) is greater') | +----------------------------------------------------------------------+ | (5 × 3 - 2) is greater | +----------------------------------------------------------------------+ 1 row in setThe following example uses the
DECODEfunction to check whether the data contains the characterS.obclient> SELECT DECODE(INSTR('CLARK','S'), 0, 'S not contained', 'S contained') "CLARK", DECODE(INSTR('KING','S'), 0, 'S not contained', 'S contained') "KING", DECODE(INSTR('MILLER','S'), 0, 'S not contained', 'S contained') "MILLER", DECODE(INSTR('ADAMS','S'), 0, 'S not contained', 'S contained') "ADAMS", DECODE(INSTR('FORD','S'), 0, 'S not contained', 'S contained') "FORD", DECODE(INSTR('JONES','S'), 0, 'S not contained', 'S contained') "JONES" FROM DUAL; +-------------+-------------+-------------+----------+-------------+----------+ | CLARK | KING | MILLER | ADAMS | FORD | JONES | +-------------+-------------+-------------+----------+-------------+----------+ | S not contained | S not contained | S not contained | S contained | S not contained | S contained | +-------------+-------------+-------------+----------+-------------+----------+ 1 row in set