CONCAT Example
We want a short profile line with name and city:
SELECT id,
name || ' · ' || city AS entry
FROM customers
ORDER BY id
LIMIT 5;Check the result with small data
We want a display label with name and city.
WITH customers(name, city) AS (
VALUES ('Amy','London'),('Liam','Leeds')
)
SELECT name || ' - ' || city AS entry
FROM customers ORDER BY entry;| entry |
|---|
| Amy - London |
| Liam - Leeds |
In SQLite and PostgreSQL, || joins strings. Other engines can use CONCAT. The calculated label is not saved automatically.
Check your understanding: What would happen in SQLite if city were NULL?
Show answer
The label would be NULL: in SQLite, || with a NULL operand returns NULL. To avoid it, use COALESCE(city, 'without city') before concatenating.
How CONCAT is used
Add the spaces and separators yourself. Joining name and city without a separator would produce values like Amy TorresMadrid.
What changes from case to case
Behaviour differs between engines and expressions. With SQLite's || operator, if one of the pieces is NULL, the whole expression is NULL:
SELECT id, name || ' · ' || email AS contact
FROM customers
WHERE id IN (1, 4)
ORDER BY id;| id | contact |
|---|---|
| 1 | Amy Wilson · amy@example.test |
| 4 | NULL |
If you want a fallback value, you can use COALESCE(email, 'no email'). Do not add it automatically: decide whether replacing the missing value represents the information correctly.
Common mix-ups with CONCAT
Forgetting the separator or NULL handling
SELECT name || city FROM customers;The query is valid, but the result does not separate the parts. On top of that, a null part can make the whole expression null, depending on the syntax and the engine.