DECODE Function - SQL


Overview


The DECODE function in SQL compares an expression to a series of values and returns the corresponding result when a match is found. It acts like a simplified CASE or IF-THEN-ELSE statement.

Example:

SELECT DECODE(department_id,
                         1, 'Business Operations',
                         2, 'Software Engineering',
                         3, 'Management',
                         5, 'Security',
                         'Other')
FROM company.employees

Syntax:

SELECT DECODE(expression, search1, result1, search2, result2, ...default_result)
FROM table

expression is the value to compare against the search values.

search1, search2, ... are the values to compare to the expression.

result1, result2, ... are the results returned if the expression matches the corresponding search value.

default_result is the result returned if no match is found. This part is optional.