[Oracle] - use of decode function

        

Table of contents

1. Use decode to judge the string or value

2. Use the decode joint sign() function to compare the size        

3. Use the decode function to segment

4. Use decode to realize row and column conversion of tables or views

5. Use the decode function to search for strings using expressions


        DECODE is an exclusive function provided by Oracle Corporation, and it is a very powerful function. It's not standard for SQL, but it's very useful for performance.

        Common scenarios for the decode function:

1. Use decode to judge the string or value

        1.1DECODE(value,if1,then1,if2,then2,if3,then3,...,else)

The meaning of sql is:


IF 条件=值1 THEN
    RETURN(value 1)
ELS IF 条件=值2 THEN
    RETURN(value 2)
ELS IF 条件=值n THEN
    RETURN(value 3)
ELSE
    RETURN(default)
END IF

sql code test

When the value of the field sex is 1, the value is male, when the value is 0, it is female, otherwise other values ​​are other;

(Note: the following output results must all be of the same type)

select decode(sex,1,'boy',0,'girl','other')

select decode(sex,'boy',1,'girl',0,-1)

2. Use the decode joint sign() function to compare the size        

decode(sign(var1-var2),-1,var1,var2)

Introduce the usage of sign() function

The sign() function returns 0, 1, -1 according to whether the value in the parentheses is 0, positive or negative

sql test

select decode(sign(100-90),1,'100',-1,'90','equal') -- the output result is 100

3. Use the decode function to segment

A score greater than 90 is considered excellent, a score between 80-90 is considered good, and a score below 80 is considered to be improved;

sql test

select decode(sign(score-90),1,'excellent',0,'excellent',-1,decode(sign(score-80),1,'good',0,'good',-1,' To be improved'))

4. Use decode to realize row and column conversion of tables or views

sql test

SELECT

SUM(DECODE(ENAME,'SMITH',SAL,0)) SMITH,

SUM(DECODE(ENAME,'ALL',SAL,0)) ALL,

SUM(DECODE(ENAME,'WARD',SAL,0)) WARD,

SUM(DECODE(ENAME,'JONES',SAL,0)) JONES,

SUM ( DECODE ( NAME , ' MARTIN ' , SAL , 0 )) MARTIN FROM EMP

The output is as follows

SMITH ALLEN WARD JONES MARTIN

800      1600    1250     2975    1250

5. Use the decode function to search for strings using expressions

decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n, default)

The decode function compares the expression and the search word, and returns a result of 1 if they match, a default value of 0 if they do not match, or a null value if no default value is defined.

sql test

SELECT ENAME,SAL,DECODE(INSTR(ENAME, 'S'),0,'does not contain s','contains s') AS INFO FROM EMP

output result

SMITH 800 contains s

ALLEN 1600 does not contain s

WARD 1250 does not contain

JONES 2975 contains s

MARTIN 1250 does not contain s

BLAKE 2850 does not contain s

CLARK 2450 does not contain s

SCOTT 3000 contains s

KING 5000 does not contain s

TURNER 1500 does not contain s

Adams 1100 contains s

Guess you like

Origin blog.csdn.net/m0_64210833/article/details/129238016