Use DECODE to map a column value to a result in a single, compact expression. It compares an input against a series of search-result pairs and returns the first match — or a default value if nothing matches.
DECODE is an Oracle-compatible SQL expression that performs conditional value mapping. It works like a simplified CASE expression but with a more concise syntax, reducing code for repeated equality checks.
Prerequisites
Your PolarDB for PostgreSQL (Compatible with Oracle) cluster must run minor engine version 2.0.14.17.36.0 or later.
To check the version, run SHOW polardb_version; or view it in the console. To upgrade, see Upgrade the minor engine version.
Syntax
DECODE(expr, search1, result1 [, search2, result2] ... [, default])Parameters
expr : The input expression to evaluate. Typically a column, but can be a subquery or any valid expression.
searchN : A value to compare against expr. Comparisons are performed in order, and only the first match is returned.
resultN : The value returned when expr matches the corresponding searchN.
default : (Optional) The value returned when expr matches no search value. If omitted and no match is found, DECODE returns NULL.
How it works
Evaluate
expr.Compare
exprtosearch1,search2, and so on, in order.Return the
resultfor the first matchingsearch, then stop.If no
searchmatches, returndefault. Ifdefaultis omitted, returnNULL.
Features
Concise syntax:
DECODEhas a more compact syntax than aCASEexpression, significantly reducing the amount of code for multiple simple equality checks.High readability: The
search-resultpair format is intuitive and easy to understand, especially for common value-mapping scenarios.
Examples
The following examples use these tables:
-- Create an inventory table
CREATE TABLE inventories (
product_id NUMBER,
warehouse_id NUMBER,
quantity NUMBER
);
-- Insert inventory data
INSERT INTO inventories VALUES (1774, 1, 100);
INSERT INTO inventories VALUES (1775, 2, 200);
INSERT INTO inventories VALUES (1776, 3, 150);
INSERT INTO inventories VALUES (1777, 4, 300);
INSERT INTO inventories VALUES (1778, 5, 250);
-- Create an employees table
CREATE TABLE employees (
employee_id NUMBER,
department_id NUMBER,
salary NUMBER,
job_id VARCHAR2(10)
);
-- Insert employee data
INSERT INTO employees VALUES (100, 10, 5000, 'IT_PROG');
INSERT INTO employees VALUES (101, 20, 6000, 'SA_REP');
INSERT INTO employees VALUES (102, 30, 4500, 'ST_CLERK');
INSERT INTO employees VALUES (103, 10, 7000, 'IT_PROG');Map values
Map a numeric ID to a human-readable label. This query maps each warehouse_id to a location name:
SELECT
product_id,
warehouse_id,
DECODE(warehouse_id,
1, 'Southlake',
2, 'San Francisco',
3, 'New Jersey',
4, 'Seattle',
'Non domestic') AS "Location"
FROM inventories
WHERE product_id < 1779
ORDER BY product_id;Output:
product_id | warehouse_id | Location
------------+--------------+---------------
1774 | 1 | Southlake
1775 | 2 | San Francisco
1776 | 3 | New Jersey
1777 | 4 | Seattle
1778 | 5 | Non domestic
(5 rows)warehouse_id 5 has no matching search value, so DECODE returns the default 'Non domestic'.
Use DECODE in a calculation
Embed DECODE in an arithmetic expression to apply different coefficients per condition. This query calculates employee bonuses based on department_id:
SELECT
employee_id,
salary,
DECODE(department_id,
10, salary * 0.15, -- IT department: 15%
20, salary * 0.12, -- Sales department: 12%
30, salary * 0.10, -- Operations department: 10%
salary * 0.05) AS bonus -- All other departments: 5%
FROM employees;Output:
employee_id | salary | bonus
-------------+--------+-------
100 | 5000 | 750
101 | 6000 | 720
102 | 4500 | 450
103 | 7000 | 1050
(4 rows)Handle no-match with no default
When no search value matches and no default is provided, DECODE returns NULL:
SELECT
employee_id,
department_id,
DECODE(department_id,
10, 'IT Department',
20, 'Sales Department'
-- No match for department_id = 30, and no default
) AS dept_name
FROM employees;Output:
employee_id | department_id | dept_name
-------------+---------------+------------------
100 | 10 | IT Department
101 | 20 | Sales Department
102 | 30 |
103 | 10 | IT Department
(4 rows)The employee with department_id 30 has no match and no default, so dept_name is NULL.
Compatibility notes
In Oracle, the return value's data type and length are determined by all result and default parameters. PolarDB provides the polar_enable_decode_type_mode parameter to emulate this Oracle behavior.
When polar_enable_decode_type_mode is enabled, the following rules apply:
All arguments must be a string literal,
NULL, a table column, or a subquery.The type length of each
resultargument cannot be unlimited. TheTEXTtype is not allowed.The return type length equals the maximum type length among all
resultarguments.
Example: verify return type length
This example creates a view and confirms that the return type length of DECODE is the maximum length across all result arguments.
-- Enable Oracle-compatible type mode
SET polar_enable_decode_type_mode = ON;
-- job_id is VARCHAR2(10), length 10
-- 'AA' has length 2, 'BBB' has length 3
-- The final return type length should be the maximum: 10
CREATE OR REPLACE VIEW decode_char_type_test AS
SELECT
DECODE('A',
'A', 'AA', -- Length 2
'B', 'BBB', -- Length 3
job_id -- Length 10
) AS a
FROM employees;
-- Check the view's column metadata
SELECT
column_name,
data_type,
character_maximum_length
FROM information_schema.columns
WHERE table_name = 'decode_char_type_test'
ORDER BY ordinal_position;Output:
column_name | data_type | character_maximum_length
-------------+-------------------+--------------------------
a | character varying | 10
(1 row)character_maximum_length is 10, confirming the return type length matches the longest result argument.