invantive documentationpart of invantive cloud
Invantive Support ForumsLog In / Sign Up

DECODE

Syntax

%%{init: {
  'theme': 'base',
  'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
  'themeVariables': {
    'fontSize': '11px',
    'fontFamily': 'Arial'
  }
}}%%
flowchart LR
    Start((START)) --> DECODE[DECODE]:::quoted
    DECODE --> End((END))

Purpose

The DECODE SQL function is a function expression similar to: if...then...elif...then...else. The expression is compared with all search values. Upon first match, the function evaluates to the result following the search. If no match found, the function evaluates to the optional default. When no default is present, the function evaluates to null.

Parameters:

  • Expression (ANY): the value to compare with. Is automatically converted to the datatype of the first search value.
  • Search1 (ANY): if Expression is equal to Search, the function evaluates to the result1.
  • Result1 (ANY): the value returned if expression equals to search
  • Searchn (optional, ANY): if Expression is equal to Search, the function evaluates to resultn.
  • Resultn (optional, ANY): the value returned if expression equals to search
  • Default (optional, ANY): if no matches are found, default will be returned.

Returns: the result of the first match and otherwise the default value as ANY.

Examples

The following example retrieves result for the search value y:

select decode('y', 'y', 'vijf', 'y', 'zes')

-------------------
vijf

The following example retrieves result for the search value y:

select decode('y', 5, 'vijf', 9, 'nine', 'y', 'yes', 'default')

-------------------
yes

The following example retrieves result for the search value unknown:

select decode('unknown', 5, 'vijf', 9, 'nine', 'y', 'yes', 'default')

-------------------
default