Conditional functions
Conditional functions return one of their arguments depending on a condition, most often on whether a value is NULL. For the general CASE expression, see Conditional expressions.
Result type
Section titled “Result type”When the arguments that can be returned have different types, CoraDB converts them all to the type with the highest priority:
CHAR<VARCHARBIT<VARBITSHORT<INT<BIGINT<NUMERIC<FLOAT<DOUBLEDATE<TIMESTAMP<DATETIME
For example, COALESCE(a, b, c, d) with an INT, a BIGINT, a SHORT and a FLOAT argument returns a FLOAT. If the arguments cannot all be converted to one type (for example numbers and dates), they are converted to VARCHAR. NUMERIC results keep enough decimal places for every possible result.
COALESCE
Section titled “COALESCE”COALESCE(expression [, expression ...])Returns the first argument that is not NULL, or NULL if all are NULL. COALESCE(a, b) is the same as CASE WHEN a IS NOT NULL THEN a ELSE b END.
Return type: the common type of the arguments (see Result type).
Show the shipping date, or the order date for orders that have not shipped:
SELECT id, shipped_date, COALESCE(shipped_date, order_date) AS status_dateFROM ordersWHERE id IN (1, 455, 456)ORDER BY id;| id | shipped_date | status_date |
|---|---|---|
| 1 | 2025-10-20 | 2025-10-20 |
| 455 | NULL | 2025-12-30 |
| 456 | 2025-01-04 | 2025-01-04 |
SELECT COALESCE(NULL, NULL, 'third', 'fourth'), COALESCE(NULL, 10, 2.5) FROM db_root;| coalesce(null, null, 'third', 'fourth') | coalesce(null, 10, 2.5) |
|---|---|
| third | 10.0 |
DECODE
Section titled “DECODE”DECODE(expression, search, result [, search, result ...] [, default])Compares expression with each search value in turn and returns the result of the first one that is equal. Returns default if none matches, or NULL if there is no default. DECODE(a, b, c, d, e, f) is the same as:
CASE WHEN a = b THEN c WHEN a = d THEN e ELSE fENDexpression and the search values must be comparable. Return type: the common type of the result and default values.
SELECT product_id, discount_pct, DECODE(discount_pct, 0, 'full price', 25, 'quarter off', 'other') AS pricingFROM order_itemsWHERE order_id = 455ORDER BY product_id;| product_id | discount_pct | pricing |
|---|---|---|
| 123 | 0 | full price |
| 335 | 0 | full price |
| 1795 | 25 | quarter off |
When the results are numbers with different scales, the result keeps all their decimal places. Without a default, a value that matches nothing returns NULL:
SELECT DECODE(2, 1, 1, 2, 1.2345, 1.234567890), DECODE(3, 1, 'one', 2, 'two') FROM db_root;| decode(2, 1, 1, 2, 1.2345, 1.234567890) | decode(3, 1, 'one', 2, 'two') |
|---|---|
| 1.234500000 | NULL |
If the results cannot be converted to a common type, the statement fails, for example DECODE(a, 1, 'one', 2, 1.2345) with Cannot coerce 'one' to type double.
GREATEST
Section titled “GREATEST”GREATEST(expression [, expression ...])Returns the greatest of its arguments, which must have comparable types. If any argument is NULL, the result is NULL. Unlike the aggregate MAX, which compares the values of one column across rows, GREATEST compares several values in the same row.
Return type: the common type of the arguments.
LEAST(expression [, expression ...])Returns the smallest of its arguments; otherwise like GREATEST.
SELECT id, unit_price, units_in_stock, GREATEST(unit_price, units_in_stock) AS greatest_value, LEAST(unit_price, units_in_stock) AS least_valueFROM productWHERE id IN (29, 123, 1795)ORDER BY id;| id | unit_price | units_in_stock | greatest_value | least_value |
|---|---|---|---|---|
| 29 | 123.79 | 0 | 123.79 | 0.00 |
| 123 | 45.00 | 95 | 95.00 | 45.00 |
| 1795 | 29.00 | 31 | 31.00 | 29.00 |
The arguments can also be dates or strings:
SELECT GREATEST(DATE'2025-01-31', DATE'2025-12-01'), LEAST('banana', 'apple', 'cherry'), GREATEST(1, NULL, 3)FROM db_root;| greatest(date '2025-01-31', date '2025-12-01') | least('banana', 'apple', 'cherry') | greatest(1, null, 3) |
|---|---|---|
| 2025-12-01 | apple | NULL |
IF(condition, expression1, expression2)Returns expression1 if condition is true, and expression2 if it is false or NULL. The two expressions must have the same type or a common type; if one of them is the NULL literal, the result has the type of the other. IF(a, b, c) is the same as CASE WHEN a IS TRUE THEN b ELSE c END.
SELECT id, units_in_stock, IF(units_in_stock = 0, 'sold out', 'in stock') AS availabilityFROM productWHERE id IN (5, 1795)ORDER BY id;| id | units_in_stock | availability |
|---|---|---|
| 5 | 0 | sold out |
| 1795 | 31 | in stock |
IF can also be used in a WHERE clause or any other place that takes an expression.
IFNULL, NVL
Section titled “IFNULL, NVL”IFNULL(expression1, expression2)NVL(expression1, expression2)Return expression1 if it is not NULL, otherwise expression2. The two functions are the same, and the same as COALESCE(expression1, expression2).
Return type: the common type of the arguments.
SELECT id, shipped_date, NVL(shipped_date, DATE'2026-01-05') AS ship_or_planned, IFNULL(CAST(shipped_date AS VARCHAR), 'not shipped') AS ship_textFROM ordersWHERE id IN (1, 455)ORDER BY id;| id | shipped_date | ship_or_planned | ship_text |
|---|---|---|---|
| 1 | 2025-10-20 | 2025-10-20 | 10/20/2025 |
| 455 | NULL | 2026-01-05 | not shipped |
A date and a string have no common type other than VARCHAR, which is why the date is cast explicitly in ship_text.
NVL2(expression1, expression2, expression3)Returns expression2 if expression1 is not NULL, and expression3 if it is NULL. Return type: the common type of expression2 and expression3.
SELECT id, NVL2(shipped_date, 'shipped', 'pending') AS stateFROM ordersWHERE id IN (1, 455)ORDER BY id;| id | state |
|---|---|
| 1 | shipped |
| 455 | pending |
ISNULL
Section titled “ISNULL”ISNULL(expression)Returns 1 if expression is NULL and 0 otherwise, like the expression IS NULL predicate. Return type: INT.
SELECT COUNT(*) FROM orders WHERE ISNULL(shipped_date);| count(*) |
|---|
| 9 |
NULLIF
Section titled “NULLIF”NULLIF(expression1, expression2)Returns NULL if the two arguments are equal, and expression1 otherwise. NULLIF(a, b) is the same as CASE WHEN a = b THEN NULL ELSE a END. It is useful to turn a placeholder value, such as 0 or an empty string, into NULL:
SELECT product_id, NULLIF(discount_pct, 0) AS discountFROM order_itemsWHERE order_id = 455ORDER BY product_id;| product_id | discount |
|---|---|
| 123 | NULL |
| 335 | NULL |
| 1795 | 25 |
The comparison follows the usual rules: 1 and 1.000 are equal, while 'A' and 'a' are different in a case-sensitive collation:
SELECT ISNULL(NULL), ISNULL(0), NULLIF(1, 1.000), NULLIF('A', 'a'), NVL2(NULL, 1, 2) FROM db_root;| isnull(null) | isnull(0) | nullif(1, 1.000) | nullif('A', 'a') | nvl2(null, 1, 2) |
|---|---|---|---|---|
| 1 | 0 | NULL | A | 2 |