conditional statement in oracle

case when then and decode usage in oracle
1. case … when … then syntax: 
– Writing 1: 
case (condition) 
when value 1 then return value 1 
when value 2 then return value 2 
else default value 

– Writing 2: 
case when condition 1 then return value 1 
when condition 2 then return value 2 
else default value 
end;

Case 1:
--If the department number is 10, it will be displayed as dept10 --If the
 department number is 20, it will be displayed as dept20 --If the
 department number is 30, it will be displayed as dept30 --Otherwise
 , it will be displayed as other
 --The result of the query in this column, The column name is displayed as department

use
Writing one:
select ename,
       shall,
       case deptno
         when 10 then
          'dept10'
         when 20 then
          'dept20'
         when 30 then
          'dept30'
         else
          'other'
       end department
  from emp

Writing two:
select ename,
       shall,
       case
         when deptno = 10 then
          'dept10'
         when deptno = 20 then
          'dept20'
         when deptno = 30 then
          'dept30'
         else
          'other'
       end department
  from emp

In this example the conditions are all equal and the result is the same.
If it is unequal or there are multiple expressions, only the second one can be used, for example:
select ename,sal,
case when deptno= 10 or deptno = 20 or deptno = 30 then 'dept'||deptno end dept
from emp;

select ename,sal,
case when deptno<=20 then 'dept'||deptno end dept
from emp;

select ename,
       shall,
       case
         when sal > 0 and sal <= 1500 then
          'level1'
         when sal > 1500 and sal <= 2500 then
          'level2'
         when sal > 2500 and sal <= 4500 then
          'level3'
         else
          'level4'
       end sal_level
  from emp
 order by sal desc;

Two.decode function: 
decode(condition, value 1, return value 1, value 2, return value 2, ...., default)
Use the decode function to implement case 1:
select ename,
       shall,
       decode(deptno, 10, 'dept10', 20, 'dept20', 30, 'dept30', 'other') department
  from emp

The result is the same as using case when above, but this sentence looks much more concise

Extended usage of decode() function:
1. Use with the sign function to compare the size:
 -- get the smaller value of arg1 and arg2 
语法:select decode(sign(arg1-arg2),-1,arg1,arg2) from dual;  
Example: select decode(sign( 3-5),1,3,5) from dual --5 Note: The sign() function returns 0, 1, -1  
according to whether a value is 0, positive or negative.

An Update statement: to change the value of a field in the table, this statement is to change the value of " 1" to "8", "0" to "9 "
update tablename set field name = decode(field name,1,8,0,9) where field name in (1, 0 );
3. Comparison between DECODE and CASE WHEN
      1. DECODE is only available in Oracle, other databases do not support;
       2. The usage of CASE WHEN is supported by Oracle, SQL Server, and MySQL;
       3. DECODE can only be used for equality judgment, but it can be used with the sign function to perform greater than, less than, equal to Judgment, CASE
       when can be used for judgment of =,>=,<,<=,<>,is null ,is not null , etc
       ; 4.DECODE ; , in decode, null and null are equal, but in case when, you can only use is null to judge
An example is as follows:
  There is a column of comm in the emp table. If this column is null, it is displayed as 0, otherwise, it is displayed as the original value:
-- can display the result we ask for
SQL> select ename,decode(comm,null,0,comm) comma from emp;
-- null is not converted to 0, it is still null, not the result we require
SQL> select ename,(case comm when null then 0 else comm end) comm from emp;
-- this will successfully display null as 0
SQL> select ename,(case when comm is null then 0 else comm end) comm from emp;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735043&siteId=291194637