Sub topic 2
How NULL Behaves in String Expressions
Assume you know basic SELECT syntax, PostgreSQL data types, NULL, and how to run queries in psql or another SQL client. This chapter focuses on how missing values affect string expressions.
NULL means “unknown” or “missing.” It is different from an empty string ('') and from the text value 'NULL'.
| Value | Meaning |
|---|---|
| `NULL` | Missing or unknown value |
| `''` | A known string with zero characters |
| `'NULL'` | Four ordinary text characters |
The concatenation operator propagates NULL
The || operator joins strings. If either operand is NULL, the whole expression becomes NULL.
SELECT
'Hello, ' || 'Ana' AS complete_name,
'Hello, ' || NULL AS missing_name,
'' || 'Ana' AS empty_prefix;The results are:
complete_name:Hello, Anamissing_name:NULLempty_prefix:Ana
This behavior follows SQL’s general rule for unknown values. PostgreSQL cannot know what the missing text contains, so it cannot produce a known combined string.
Do not use COALESCE to hide important missing data without deciding what the missing value should mean. COALESCE returns the first non-NULL value.
SELECT
'Hello, ' || COALESCE(NULL, '') || 'Ana' AS greeting,
COALESCE(NULL, '[unknown]') AS displayed_value;Here, the first expression treats the missing value as no text. The second displays a label instead.
concat and concat_ws handle NULL differently
PostgreSQL provides functions that often make optional text easier to combine.
concat(value1, value2, ...)converts values to text and ignoresNULLarguments.concat_ws(separator, value1, value2, ...)means “concatenate with separator.” It ignoresNULLarguments and does not add separators for them.- If the separator passed to
concat_wsisNULL, the result isNULL.
SELECT
'a' || NULL AS with_operator,
concat('a', NULL, 'b') AS with_concat,
concat_ws(', ', 'x', NULL, 'z') AS with_separator;| Expression | Result | Column | Column |
|---|---|---|---|
| `'a' | NULL` | `NULL` | |
| `concat('a', NULL, 'b')` | `ab` | ||
| `concat_ws(', ', 'x', NULL, 'z')` | `x, z` |
concat_ws is useful for optional address or name parts:
SELECT concat_ws(' ', first_name, middle_name, last_name) AS full_name
FROM people;A NULL middle name does not create two spaces. However, an empty string is still a value, so it may affect the output differently.
Other string functions and NULL
Most string functions return NULL when their main string argument is NULL.
SELECT
length(NULL::text) AS text_length,
lower(NULL::text) AS lower_text,
substring(NULL::text FROM 1 FOR 3) AS text_part;Each result is NULL. The explicit cast helps PostgreSQL identify the unknown literal as text.
The same principle applies to predicates. NULL LIKE '%abc%' does not return false; it returns NULL, which a WHERE clause does not select.
Exercise
What do these expressions return: concat('A', NULL, 'B') and 'A' || NULL || 'B'?
Show answer
The first returns AB because concat skips NULL arguments. The second returns NULL because the || operator propagates NULL.