Decode function is more like an IF-ELSE-ELSIF-ENDIF kind of condition. The Decode function format has an expression and a search value to search for. If the expression matches with the search value, then a corresponding result is given back by the function.

The format of DECODE function is:

DECODE (expression, search value_1, result1,
search value_2, result2,
search value_3, result3,

It means – the code will check if the result of the expression matches with the search value_1, then gives back result1 as the output, if the result of the expression matches with search value_2, then gives back result2 as the output and so on and so forth.

Example: In order to understand DECODE function, let’s first create the required data. Use the following statements to create table and insert data.

Once the table is created, run the following statement.

The query generates the following output.

Thus, can be seen that, the query checks for the expression – if the value is 10, the function gives the word “Marketing”, if the value is 20, the function gives back “Sales” and etc..

Also, in the example above, it can be seen that no Department Name is assigned for Dept_ID 50. And thus, it returns a NULL Value.

Leave a Reply

  • (will not be published)