The DECODE function in Oracle can select a value from multiple values based on conditions. The syntax is: DECODE(expression, value1, result1, value2, result2, ..., default_result). It evaluates an expression against a range of values and returns the corresponding result if there is a match, or a default result if there is no match. DECODE supports nesting, the number of value and result parameters must appear in pairs, and the default_result parameter is optional.
Usage of DECODE in Oracle
The DECODE function is a useful tool in Oracle that allows you Selects a value from multiple values based on specified criteria. The syntax is as follows:
<code>DECODE(expression, value1, result1, value2, result2, ..., default_result)</code>
Usage details:
How it works:
DECODE function compares expression with the given value one by one. If a match is found, the result associated with that value is returned. If no match is found, default_result is returned.
Example:
<code>SELECT DECODE(customer_type, 'standard', 10%, 'premium', 20%, 'vip', 30%, 0) FROM sales_data;</code>
This query calculates customer discounts based on the value of the customer_type column. Standard customers get 10% discount, premium customers get 20% discount, and vip customers get 30% discount. If the value of the customer_type column is not within the given range, no discount will be applied.
Note:
The above is the detailed content of How to use decode in oracle. For more information, please follow other related articles on the PHP Chinese website!