DECODE(x,y(i),...,z) is a special function that decodes expressions in accordance with their values.
x |
check_expression |
Expression ( expression) for which a comparison is to be carried out with the values in y(i) |
y(i) |
search_and_result_spec |
<search_and_result_spec> ::= <search_expression>, <result_expression> (y(i)=u(i),v(i), i=1,...) Combination of the comparison value u(i) and the value v(i) that is to replace this comparison value |
z |
default_expression |
Optional default value |
u(i) |
search_expression |
Comparison value that is to be replaced by v(i) if it matches x |
v(i) |
result_expression |
Value that is to be replace u(i) |
The data types of x and u(i) must be comparable. The data types of v(i) and z must be comparable. The data types of u(i) and v(i) do not have to be comparable.
DECODE compares the values of x with the values u(i) consecutively. If a match is found, the result of DECODE is the value v(i) in the combination y(i)=u(i),v(i).
A match is present if x and u(i) are NULL values. The comparison of the special NULL value with any other value never results in a match.
If a match is not found, DECODE supplies the result of z. If z is not specified, the NULL value is the result of DECODE.
Example table: room
The room type identifiers are to be replaced in the output by an identifier declared in the DECODE function.
SELECT hno, price, DECODE (roomtype,
'SINGLE', 1, 'DOUBLE', 2, 'SUITE', 3) room_code
FROM room