Sub topic 3
Joining strings with `||`, `concat` and `concat_ws`
This chapter assumes you know basic SELECT syntax and how PostgreSQL represents missing values with NULL. PostgreSQL provides three common ways to join text: the || operator, concat, and concat_ws.
The `||` operator
The || operator joins two values in order:
SELECT 'Ada' || ' Lovelace' AS full_name;The result is:
Ada LovelaceYou can join more than two values by chaining the operator:
SELECT first_name || ' ' || last_name AS full_name
FROM customers;The operator does not add spaces or other separators automatically. You must include them explicitly.
|| has strict NULL behavior. If either operand is NULL, the whole expression becomes NULL:
SELECT 'a' || NULL AS result;The result is NULL, not a. This behavior can be useful when a missing component should make the entire result unavailable, but it often causes unexpected missing labels.
`concat` ignores `NULL` arguments
The concat function accepts a variable number of arguments and joins their text representations. It skips NULL arguments:
SELECT concat('Ada', ' ', 'Lovelace') AS full_name,
concat('Ada', NULL, 'Lovelace') AS without_middle;The results are Ada Lovelace and AdaLovelace. concat skips the missing value, but it does not know that a separator should go around it. If you pass the separator as a separate argument, the separator remains.
This difference matters when building values from optional columns:
SELECT concat(first_name, ' ', middle_name, ' ', last_name) AS full_name
FROM customers;If middle_name is NULL, this can produce two adjacent spaces. It does not remove the surrounding separators.
`concat_ws` joins with a separator
concat_ws means “concatenate with separator.” Its first argument is the separator. Later arguments are joined with that separator, and NULL arguments are skipped along with their separators:
SELECT concat_ws(', ', 'Lovelace', NULL, 'London') AS description;The result is:
Lovelace, LondonThis makes concat_ws a good choice for optional name parts, addresses, and labels:
SELECT concat_ws(' ', first_name, middle_name, last_name) AS full_name
FROM customers;If middle_name is NULL, the result contains one space between the first and last names.
The separator itself is different. A NULL separator makes the entire result NULL:
SELECT concat_ws(NULL, 'a', 'b') AS result;An empty separator is valid and produces ab.
Choosing the operation
| Situation | Best choice | `NULL` behavior | Column | Column |
|---|---|---|---|---|
| Every component must exist | ` | ` | Any `NULL` makes the result `NULL` | |
| Join values while skipping missing ones | `concat` | Skips `NULL` arguments | ||
| Join optional values with clean separators | `concat_ws` | Skips `NULL` values and their separators |
The following diagram summarizes the decision:
Exercise
Write a query that returns first_name, middle_name, and last_name separated by single spaces, without extra spaces when middle_name is NULL.
Show answer
SELECT concatws(' ', firstname, middlename, lastname) AS full_name FROM customers;