Shape / columns · All operations

Add a column by condition

If, case or switch expression to a column.

Status

In arcform

Runs as a SQL step

arcform carries it as a sql: step today.

Shape of the result
one row out per row in, with columns added or changed
Level
one clause of SQL

The DuckDB step

A banded column

It reads the orders table the proofs share. Create it first:

SQL
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);

Then run the statement:

SQL
CREATE TEMP TABLE result AS
SELECT order_id, CASE WHEN amount IS NULL THEN 'unknown' WHEN amount >= 300 THEN 'large' WHEN amount >= 100 THEN 'medium' ELSE 'small' END AS band FROM orders;
SELECT * FROM result;
SELECT CASE WHEN ((SELECT map_from_entries(list((band, n) ORDER BY band)) = MAP {'large': 3, 'medium': 2, 'small': 4, 'unknown': 1} FROM (SELECT band, count(*) AS n FROM result GROUP BY band))) THEN 'as expected' ELSE error('the bands do not count 3 large, 2 medium, 4 small and 1 unknown') END AS verdict;
  • It needs nothing installed beyond DuckDB itself.
  • The statement checks its own result and raises an error when it is wrong: “the bands do not count 3 large, 2 medium, 4 small and 1 unknown”.
  • It passed on DuckDB v2.0.0 (Cyanoptera) d54cbbe3df.

What other tools call it

Meridian's classification; an independent blind rating agreed on 86.2% of 312 rows (exact class, version 2).

Apache NiFi (1)
  • Boolean Logicread 2026-09-27

    16 functions: isNull, notNull, isEmpty, equals, equalsIgnoreCase, …

Apache SeaTunnel (1)
  • System Functionsread 2026-09-27

    10 functions: CAST, TRY_CAST, COALESCE, IFNULL, NULLIF, …

Azure Data Factory (1)
  • Logical functionsread 2026-09-27

    9 functions: and, equals, greater, greaterOrEquals, if, …

EasyMorph (1)
  • Ruleread 2026-09-27

    Set a new or existing column from ordered IF-THEN-ELSE conditions, first match wins

Ibis (3)
  • IfElseread 2026-09-27

    Pick one of two values by a boolean condition

  • SearchedCaseread 2026-09-27

    CASE that tests a list of boolean conditions in order

  • SimpleCaseread 2026-09-27

    CASE that compares one expression against a list of values

KNIME Analytics Platform (4)
  • Column Comparatorread 2026-09-27

    Compare two columns row by row and write a chosen value or tag per outcome

  • Rule Engineread 2026-09-27

    Set a column from ordered first-match business rules evaluated per row

  • Set a column from first-match business rules supplied in a second table

  • Ruleset Editorread 2026-09-27

    Write first-match rules, apply them to rows and emit them as a PMML RuleSet model

OpenRefine (1)
  • Controlsread 2026-09-27

    13 controls, which branch, loop and test inside an expression: if, with, filter, forEach, isBlank

Polars (2)
  • 7 functions: CASE, COALESCE, GREATEST, IF, IFNULL and others; SQL conditional functions

  • whenread 2026-09-27

    Start a when-then-otherwise chain that picks a value by conditions

Tableau Prep (2)
  • Keywords (group)read 2026-09-27

    12 keywords: IF, CASE, ELSEIF, AND, NOT, ...; conditional and boolean syntax, ASC and DESC

  • 5 functions: IFNULL, IIF, ISDATE, ISNULL, ZN; null tests and inline conditionals

Trifacta (7)
  • Case Transformread 2026-09-27

    If/then/else or case logic producing a column

  • 10 functions: ISEVEN, IN, MATCHES, EQUAL, GREATERTHAN; compare two values, return true or false

  • Comparison Operatorsread 2026-09-27

    Greater than, less than, equals and not-equals operators returning true or false

  • Conditional columnread 2026-09-27

    New column from if-then-else or case conditions

  • 3 functions: AND, OR, NOT; combine boolean expressions

  • Logical Operatorsread 2026-09-27

    and, or and not operators (&&, ||, !) combining boolean expressions

  • Ternary Operatorsread 2026-09-27

    If/then/else as a ternary expression; the documentation says to use IF instead

Background

The SQL CASE expression is a generic conditional expression, similar to if/else statements in other programming languages.[1]

When to use it

If the ELSE clause is omitted and no condition is true, a CASE expression returns NULL.[1][2]

In a simple CASE expression, a WHEN NULL branch is never chosen, because a comparison with NULL results in Unknown.[3] A searched CASE expression can use predicates such as IS NULL and IS NOT NULL in its conditions.[3] To give missing values a label of their own, use the searched form and test IS NULL before the other conditions. (Meridian's view)

SQL Server allows only 10 levels of nesting in CASE expressions.[4]

The method

SQL has had the case expression since SQL-92, and the standard calls its most general form a searched case.[5] The simple form of CASE is similar to the switch statement in C.[1]

In PostgreSQL, a CASE construct used to force evaluation order will defeat optimisation attempts, so it should only be done when necessary.[6] In SQL Server, a CASE expression returns the highest precedence type from the set of types in its result expressions and its ELSE expression.[4]

Compared with the alternatives

In DuckDB, a CASE expression with a single condition can be written as IF(cond, a, b).[2] COALESCE and NULLIF are abbreviations for searched CASE expressions.[3] COALESCE returns the value of the first non-NULL operand, working from left to right.[5] NULLIF has two operands and returns NULL if the operands have the same value.[5] Reach for COALESCE when the only question is whether a value is missing, and keep CASE for rules that test the values themselves. (Meridian's view)

Used in

8 Protocols use this operation: