How to write DECODE function effectively with little effort

DECODE SQL function in DB2
[DECODE SQL function in DB2]
The DECODE function in SQL handles logic related scenarios. This function works similar to using if -else-endif conditions in any high level programming languages.

Syntax:

DECODE(expression1,expression2,result-expression,else-expression).

The expression1 is equal to “Column name”. And it compares the next value, if it matches, then it replaces with third value.

Examples:

DECODE(c1, 7, 'a', 
 6, 'b', 'c')
DECODE(c1, var1, 'a', 
 var2, 'b')
SELECT ID, DECODE(STATUS, 
 'A', 'Accepted',
 D', 'Denied',
CAST(NULL AS VARCHAR(1)), 
 'Unknown', 'Other')
FROM CONTRACTS
Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.