在 SELECT 语句中使用函数 / 其它函数 |
DECODE(test, a, a_value, b, b_value, ..., n, n_value, exp_m )
在通常情况下,当 a 等于 test 时 DECODE 函数返回 a_value,当 b 等于 test 时,返回 b_value,当 n 等于 test 时返回 n_value 。
如果有若干表达式与 test 匹配,那么 DECODE 返回找到的第一个表达式的 n_value。如果没有表达式与 test 匹配,那么 DECODE 返回 exp_m;;如果没有表达式与 test 匹配并且不存在 exp_m,那么 DECODE 返回 NULL。
图: 查询
SELECT emp_id, evaluation FROM employee;
图: 查询结果
emp_id evaluation 012233 great 012344 poor 012677 NULL 012288 good 012555 very good
图: 查询
SELECT emp_id, DECODE(evaluation, "poor", 0, "fair", 25, "good", 50, "very good", 75, "great", 100, -1) AS evaluation FROM employee;
图: 查询结果
emp_id evaluation 012233 100 012344 0 012677 -1 012288 50 012555 75 ⋮