개인적인 정리

ORACLE DECODE() 본문

DB/ORACLE

ORACLE DECODE()

yeon.Biju 2020. 3. 26. 11:19

오라클 DECODE()

 

DECODE(expr, search, result, default)

 

DECODE(expr, search, result,

                    search, result,

                    search, result,             

               default);

와 같은 형태도 가능하여 여러개를 한번에 비교할 수 있다.

 

 

 

DECODE compares expr to each search value one by one. If expr is equals to a search, then Oracle Database returns the corresponding result. If no match is found, then Oracle returns default. If default is omitted, then Oracle returns null.

The arguments can be any of the numeric type(NUMBER, BINARY_FLOAT, or BINARY_DOUBLE) or character types.

 

  • If expr and search are character data, then Oracle compares them using nonpadded comparison semantics, expr, search, and result can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2. The string returned is of VARCHAR2 data type and is in the same character set as the first result parameter.
  • If the first search-result pair numeric, the Oracle compares all search-result expressions and the first expr to determine the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

 

Oracle automatically converts expr and each search value to the data type of the first search value before comparing. Oracle automatically converts the return value to the same data type as the first result. If the first result has the data type CHAR or if the first result is null, then Oracle converts the return value to the data type VARCHAR2.

 

In a DECODE function, Oracle considers two nulls to be equivalent. If expr is null, then Oracle returns the result of the first search that is also null.

 

The maximum number of components in the DECODE function including expr, searches, results, and default is 255.

 

SQL > SELECT DECODE ('88', 
                   'B', 'FFF',
                   'C', 'FFF',
                   'D', 'FFF',
                   'a', 'TTT',
                '디폴트'   
              ) FROM DUAL ; 

   --> 디폴트

 

SQL > SELECT DECODE ('a',  
                   'B', 'FFF', 
                   'C', 'FFF', 
                   'D', 'FFF', 
                   'a', 'TTT', 
                '디폴트'    
              ) FROM DUAL ;

   --> TTT

 

 

'DB > ORACLE' 카테고리의 다른 글

ORACLE EXP()  (0) 2020.03.30
ORACLE EMPTY_BLOB(), EMPTY_CLOB()  (0) 2020.03.30
ORACLE DENSE_RANK()  (0) 2020.03.26
ORACLE DELETEXML()  (0) 2020.03.26
ORACLE CURRENT_TIMESTAMP()  (0) 2020.03.26
ORACLE CURRENT_DATE()  (0) 2020.03.26
ORACLE COUNT()  (0) 2020.03.26
ORACLE COS()  (0) 2020.03.26
Comments