Faster than a speeding bullet, more powerful than a locomotive. Perhaps not, but Oracle's CASE expression can do all that the DECODE function does, plus a whole lot more.
DECODE is considered the most powerful function in Oracle. Oracle 8i release introduced the CASE expression. The Oracle CASE statements can do all that DECODE does plus lot of other things including IF-THEN analysis, use of any comparison operator and checking multiple conditions, all in a SQL query itself. Moreover, using the CASE function, multiple conditions provided in separate SQL queries can be combined into one, thus avoiding multiple statements on the same table (example given below). The function is available from Oracle 8i onwards.
Oracle CASE expression syntax is similar to an IF-THEN-ELSE statement. Oracle checks each condition starting from the first condition (left to right). When a particular condition is satisfied (WHEN part) the expression returns the tagged value (THEN part). If none of the conditions are matched, the value mentioned in the ELSE part is returned. The ELSE part of the expression is not mandatory-- CASE expression will return null if nothing is satisfied. Here is the basic syntax of an Oracle CASE When statement:
The following examples will make the use of CASE expression more clear, using Oracle CASE select statements.
E.g. Returning categories based on the salary of the employee.
E.g. The requirement is to find out the count of employees for various conditions as given below. There are multiple ways of getting this output. Five different statements can be written to find the count of employees based on salary and commission conditions, or a single select having column-level selects could be written.
expression, the above multiple statements on the same table can be avoided using Oracle select case.
E.g. CASE expression can also be nested.
E.g. The data types of the returned values should be the same. In the example below, one argument is assigned a numeric value resulting in an error.
COALESCE and NULLIF functions
Oracle provides two more functions that carry out a functionality that is similar to the CASE expression in certain scenarios. We can use these in conjunction with or as a variety of the CASE expression.
COALESCE returns the first not null value in a given list of values.
E.g. Returning the first not null value available in four columns present in a table.
E.g. The above example will return the same result as the below statement with the CASE expression.
The NULLIF function compares two values and does the following.
- Returns null if both values are the same.
- Returns the first value if both values are different.
E.g. Returning the credits available for customers. The query below will return null if the TOTAL_CREDITS column is equal to the CREDITS_USED column for a customer, else it will return the TOTAL_CREDITS value.
E.g. The above example will return the same result as the statement below with CASE expression.
The maximum number of arguments that can be specified is 255, each WHEN. THEN pair is counted as two arguments. To avoid this limitation the CASE function can be nested.
This functionality is supported in PL/SQL from Oracle 9i. The CASE expression will make it easy for developers to get more information based on analysis in a single query.