Null coalescing

Give a value a fallback.

Syntax

Put ?? between the values, from the one you prefer to the last resort. A single ? means exactly the same.

value ?? fallback ?? last_resort
value ? fallback
LET rows = LOOKUP(…) ?? fallback

Parameters and arguments

  • values

    Two or more values: a literal, a record's field, a row column, a variable, a computation, a comparison, a function result, a list or record, or a LOOKUP

    Required: Yes

Examples

null, 0, false, an empty list and an empty record are skipped; the first filled value is returned.

Return type and behaviour

The first value that is not empty is returned. null, false, 0, an empty list and an empty record count as empty. A field or path the record does not have counts as empty too, and so does a LOOKUP that finds no rows.

A text value, including an empty text, is returned as it is, so "" ?? "x" is "". If every value is empty, the last one is returned.

Errors and exceptions
ErrorWhat happenedHow to recover
HTQL-1003A ?? was written directly after RETURN, for example RETURN null ?? "x"Put it inside a record: RETURN {"v": null ?? "x"}
Limitations
  • Write ?? inside a record value in a RETURN, in a list item, in a function argument, or as a LET's value.
  • Not supported: a bare chain directly after RETURN, such as RETURN null ?? "x". Put it inside a record: RETURN {"v": null ?? "x"}.
  • ?? and ? are the same operator. Prefer ??, which cannot be confused with anything else.
  • To fall past an empty text, compare it first: IF(name = "", "unknown", name).
Best practices
  • End the chain with a value you know is never empty.
  • Use it after a LOOKUP to give a default when no rows match.
  • Use a chain to prefer a nickname, then a name, then a fixed label.
  • Remember that an empty text is a value.