The CASE statement in Oracle is a tool for condition evaluation. Its usage includes: specifying the condition to be evaluated (WHEN). The action (THEN) to perform if the condition is true. If all conditions are false, perform the default action (ELSE, optional).
Usage of CASE statement in Oracle
The CASE statement is a method used for conditional evaluation in Oracle Powerful tool. It allows you to perform different actions based on given conditions.
Grammar:
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END
Usage:
The usage of CASE statement is as follows:
Example:
The following example uses the CASE statement to determine a grade based on a student's grade:
SELECT name, CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN 'B' WHEN score >= 70 THEN 'C' ELSE 'F' END AS grade FROM students;
CASE expression:
CASE statements can also be used as expressions, returning results evaluated based on conditions:
-- 检查一个数字是否为偶数或奇数 CASE WHEN num % 2 = 0 THEN 'Even' ELSE 'Odd' END
Nested CASE:
CASE statements can Nesting to handle more complex conditions:
SELECT CASE WHEN status = 'Active' THEN CASE WHEN balance >= 10000 THEN 'High' ELSE 'Low' END ELSE 'Inactive' END AS account_status FROM accounts;
Advantages:
Using the CASE statement has the following advantages:
The above is the detailed content of How to use case in oracle. For more information, please follow other related articles on the PHP Chinese website!