The CASE WHEN statement in Oracle is used to return different values based on conditions. Syntax: CASE WHEN condition THEN result ELSE default_result END. Usage: 1. Condition check: WHEN clause contains the condition and returned result; 2. Default result: ELSE clause specifies the default result when any condition is not met. Example: Return income_level based on salary value: salary > 5000: high income; 3000 ≤ salary ≤ 5000: middle income; salary < 3000: low income.
CASE WHEN usage in Oracle
The CASE WHEN statement is a conditional expression that is used to Group conditions return different values. It is very useful when dealing with complex data queries and operations.
Syntax:
<code class="sql">CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END</code>
Usage:
Example:
SELECT CASE
WHEN salary > 5000 THEN '高收入'
WHEN salary BETWEEN 3000 AND 5000 THEN '中等收入'
ELSE '低收入'
END AS income_level
FROM employees;
Result:
This query will return the income_level column based on the employee’s salary value:
- salary > 5000: high income
- 3000 ≤ salary ≤ 5000: middle income
- salary < 3000: low income
Note:
- The conditions in the CASE statement can use any valid SQL expression.
- CASE statements can be nested, allowing the creation of more complex branching logic.
- In some cases, the CASE statement can be replaced by the DECODE function, which has a more concise syntax.
The above is the detailed content of casewhen usage in oracle. For more information, please follow other related articles on the PHP Chinese website!