SQL DECODE function real example to your project

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

Syntax:

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

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

Related: SQL NULL values in database all you need to know

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.