ArticleDirectory
- Basic syntax
- Examples
- Coalesce and nullif Functions
- Conclusion
The difference between the expression and the as expression after the case and the end is the most obvious
-- Oracle
Select T_cx_orders. * ,
Case When Fissendmail = 1 Then Ffilepath
Else ' . '
End Downloadpath
From T_cx_orders
Where Fuserid = ' Aaa613 ' Order By Finnercode Desc
-- SQL
Select Au_fname, au_lname,
Case State
When ' CA ' Then ' California '
When ' KS ' Then ' Kansas '
When ' TN ' Then ' Tennessee '
When ' Or ' Then ' Oregon '
When ' Mi ' Then ' Michigan '
When ' In ' Then ' Indiana '
When ' MD ' Then ' Maryland '
When ' UT ' Then ' Utah '
End As Statename
From Pubs. DBO. Authors
Order By Au_lname
Decode is considered the most powerful function in Oracle. oracle 8i release introduced the case expression. the case expression 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.
Basic syntax
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 participating 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.
Case when <condition> then <value>... else <value> end
Examples
The following examples will make the use of case expression more clear.
E. g.: Returning categories based on the salary of the employee.
Select Sal, case when Sal <2000 then 'category 1 'when Sal <3000 then' Category 2 'when Sal <4000 then' Category 3 'else' category 4' end from EMP;
E. g.: the requirement is to find out the Count of employees for varous 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 cocould be written.
Select count (1) from EMP where Sal <2000 and comm is not null; select count (1) from EMP where Sal <2000 and comm is null; select count (1) from EMP where Sal <5000 and comm is not null; select count (1) from EMP where Sal <5000 and comm is null; select count (1) from EMP where SAL> 5000;
(Or)
Select (select count (1) from EMP where Sal <2000 and comm is not null) A, (select count (1) from EMP where Sal <2000 and comm is null) B, (select count (1) from EMP where Sal <5000 and comm is not null) C, (select count (1) from EMP where Sal <5000 and comm is null) d, (select count (1) From empwhere SAL> 5000) efrom dual
With case expression, the above multiple statements on the same table can be avoided.
Select count (case when Sal <2000 and comm is not null then 1 else null end), count (case when Sal <2000 and comm is null then 1 else null end ), count (case when Sal <5000 and comm is not null then 1 else null end), count (case when Sal <5000 and comm is null then 1 else null end ), count (case when Sal> 5000 then 1 else null end) from EMP;
(Or)
Select count (case when Sal <2000 and comm is not null then 1 end) cnt1, count (case when Sal <2000 and comm is null then 1 end) cnt2, count (case when Sal <5000 and comm is not null then 1 end) cnt3, count (case when Sal <5000 and comm is null then 1 end) cnt4, count (case when Sal> 5000 then 1 end) cnt5 from EMP;
E. g.: Case expression can also be nested.
Select (case when qty_less6months <0 and qty_6to12months <0 then (case when season_code in ('0', '1', '2', '3', '4 ') then 'value is negative 'else' no stock' end) When qty_1to2years <0 and qty_2to3years <0 then (case when season_code in ('A', 'B', 'C ', 'D', 'E') then' value is negative 'else' no stock' end) else 'stock available' end) stock_checkfrom primary rownum <20and qty_less6months <0 and qty_6to12months <0
E. g.: The data types of the returned values shocould be the same. In the example below, one argument is assigned a numeric value resulting in an error.
SQL> select Sal, case when Sal <2000 then 'category 1 '2 when Sal <3000 then 0 3 when Sal <4000 then 'category 3 '4 else' category 4 '5 end 6 from EMP; when Sal <3000 then 0 * error at line 2:
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.
Select coalesce (col1, col2, col3, col4) from am25;
E. g.: The above example will return the same result as the below statement with the case expression.
Select case when col1 is not null then col1 when col2 is not null then col2 when col3 is not null then col3 when col4 is not null then col4 else null end valfrom am25;
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 MERs. 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.
Select customer_name, nullif (total_credit, credits_used) from customer_credits;
E. g.: The above example will return the same result as the statement below with case expression.
Select customer_name, case when total_credits = credits_used then null else total_credits endfrom customer_credits;
Conclusion
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.