Concatenation operator ||
Last updated: 2026-11-04
Concatenation operator ||
|| joins two values into a single text result and is the only string concatenation operator in PostgreSQL (+ does not work for text). It also has an array flavour: array || array concatenates them end to end, and array || scalar appends the scalar to the end of the array. Text concatenation propagates NULL — if either side is NULL the result is NULL — so use concat() or concat_ws() when NULLs should be skipped.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 Syntax
a || b -- text concatenation
array || array -- array concatenation
array || element -- array append
Precedence: || falls in the "any other operator" tier, below `* / %` and binary `+ -` but above the comparison operators. So `1 + 2 || 'x'` is '3x' (add first) and `'a' || 'b' = 'ab'` is true (concatenate first). || is left-associative.
⚙️ Parameters
| a | Left operand: text, an array, or a scalar compatible with b; a NULL makes the text concatenation NULL. |
|---|---|
| b | Right operand: text, an array, or a scalar appendable to an array. |
Returns:Text concatenation returns text; array concatenation returns an array of the input element type.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT id, first_name || ' ' || last_name AS full_name FROM t_concat ORDER BY id
id | full_name
---+-------------
1 | Ada Lovelace
2 | NULL
(2 rows)
SELECT 'Postgre' || 'SQL' AS text_concat, ARRAY[1, 2] || ARRAY[3, 4] AS array_concat, ARRAY[1, 2] || 3 AS array_append
text_concat | array_concat | array_append
------------+--------------+-------------
PostgreSQL | {1,2,3,4} | {1,2,3}
(1 row)
📌 Key Points
- NULL propagation: text concatenation returns NULL if either side is NULL, while concat() skips NULLs.
- Two semantics: text || text yields text, array || array yields the concatenated array.
- || binds looser than arithmetic and tighter than comparison — parenthesise when mixing.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QHow do || and concat() differ?
A|| returns NULL if any operand is NULL, while concat() ignores NULL arguments. concat_ws(sep, ...) is better for joining optional columns because it also skips NULLs.