Detailed several uses decode functions

Several decode Usage
1: determining whether the decode string as
DECODE (value, if1, then1, if2, then2, if3, then3, ..., else)
meaning
IF condition THEN = value 1
    the RETURN (value 1)
ELSIF THEN condition = value 2
    the RETURN (value 2)
    ......
ELSIF condition THEN = value n-
    the RETURN (value. 3)
the ELSE
    the RETURN (default)
the END the IF


SQL test
select empno, decode (empno, 7369 , 'smith', 7499 , 10 'allen', 7521, 'ward', 7566, 'jones', 'unknow') as name from emp where rownum <=
output
7369 Smith
7499 Allen
7521 Ward
7566 Jones
7654 unknow
7698 unknow
7782 unknow
7788 unknow
7839 unknow
7844 unknow




2: Comparison of size decode
SELECT decode (Sign (-var1 var2), -. 1, var. 1, var2) from Dual
Sign () function in accordance with a value of 0, positive or negative, respectively, return 0,1, -1
sql test
select decode (sign (100-90), - 1,100,90) from dual
output
90
100-90 = 10> will return a 0, so the final value of the function decode 90
anyway
select decode (sign (100- 90), 1,100,90) from dual
output
100
100-90 = 10> 0 return 1, the judgment result is 1, the first variable return 100, the final output result 100




3: decode function segment
salary is greater than 5000 salary, salary range 3000 to 5000 is moderate, as low-paid wages less than 3000
sql test
the SELECT 
    ename, SAL,
    the DECODE (the SIGN (SAL - 5000),
            . 1,
            'High SAL',
            0,
            'High SAL',
            -. 1,
            DECODE(SIGN(sal - 3000),
                    1,
                    'mid sal',
                    0,
                    'mid sal',
                    - 1,
                    DECODE(SIGN(sal - 1000),
                            1,
                            'low sal',
                            0,
                            'low sal',
                            - 1,
                            'low sal')))
FROM
    emp
输出结果
SMITH   800   low sal
ALLEN 1600 low sal
WARD 1250 low sal
JONES 2975 low sal
MARTIN 1250 low sal
BLAKE   2850 low sal
CLARK 2450 Low SAL
SCOTT 3000 MID SAL
KING 5000 High SAL
TURNER 1500 Low SAL
on ADAMS 1100 is Low SAL
JAMES 950 Low SAL
FORD 3000 MID SAL
MILLER 1300 Low SAL


. 4: using a decode implement a table or trying ranks conversion
sql test
the SELECT 
       the SUM (the DECODE ( the ENAME, 'SMITH', SAL, 0)) SMITH,
       the SUM (the DECODE (the ENAME, 'ALLEN', SAL, 0)) ALLEN,
       the SUM (the DECODE (the ENAME, 'WARD', SAL, 0)) WARD,
       the SUM (the DECODE (the ENAME, 'JONES', SAL, 0)) JONES,
       the SUM (the dECODE (the ENAME, 'MARTIN', SAL, 0)) MARTIN the FROM the EMP
output is as follows
SMITH ALLEN WARD JONES MARTIN
  800 1600 1250 2975 is 1250


. 5: use of decode functions to use an expression to the search string
decode (expression The, search_1, result_1, search_2, result_2, ...., the search_n, result_n, default)
decode function compares the search word expressions and, if a match, return results; If not, return to default value; default if undefined value, null is returned.
sql test
the SELECT 
    the ENAME,
    SAL,
    the DECODE (INSTR (the ENAME, 'S'),
            0,
            'does not contain S',
            'comprising S') the AS the INFO
the FROM
    the EMP
output
SMITH 800 containing S
ALLEN 1600 does not contain S
WARD 1250 does not contain s
JONES 2975 is contained s
MARTIN 1250 does not contain s
BLAKE 2850 does not contain s
CLARK 2450 does not contain s
SCOTT 3000 containing s
KING 5000 does not contain s
TURNER not contain 1500 s
on ADAMS 1100 is contained s
JAMES containing 950 s
FORD 3000 does not contain s
MILLER 1300 does not contain s

Guess you like

Origin blog.csdn.net/qq_38941937/article/details/81625691