Search results
Learn how to use the Oracle CASE expression to add if-else logic to SQL statements. See examples of simple and searched CASE expressions in SELECT, UPDATE, and HAVING clauses.
You can rewrite it to use the ELSE condition of a CASE: SELECT status, CASE status WHEN 'i' THEN 'Inactive' WHEN 't' THEN 'Terminated' ELSE 'Active' END AS StatusText FROM stage.tst
7 gru 2023 · If you want to do if-else-then logic in select, where or anywhere else in a statement, you need a case expression. This is a series of when clauses that the database runs in order: For example, if you want to map exam correct percentages to grade letters according to these rules:
In a simple CASE expression, Oracle Database searches for the first WHEN... THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN... THEN pairs meet this condition, and an ELSE clause exists, then Oracle returns else_expr. Otherwise, Oracle returns null.
This Oracle tutorial explains how to use the Oracle / PLSQL CASE statement with syntax and examples. The Oracle / PLSQL CASE statement has the functionality of an IF-THEN-ELSE statement. Starting in Oracle 9i, you can use the CASE statement within a SQL statement.
CASE in SELECT & WHERE. select student_id, exam_id, percent_correct, case when percent_correct >= 90 then 'A' when percent_correct >= 80 then 'B' when percent_correct >= 70 then 'C' when percent_correct >= 60 then 'D' when percent_correct >= 50 then 'E' else 'F' end grade. from exam_results.
The simple CASE statement evaluates a single expression and compares it to several potential values. The searched CASE statement evaluates multiple Boolean expressions and chooses the first one whose value is TRUE.