Function or operator · Text, numbers and dates

CONCAT

CONCAT joins text fragments into a single value. The treatment of NULL depends on the engine and the way of concatenating.

CONCAT Example

We want a short profile line with name and city:

SQLite
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.

Self-contained query · SQLite
WITH customers(name, city) AS (
  VALUES ('Amy','London'),('Liam','Leeds')
)
SELECT name || ' - ' || city AS entry
FROM customers ORDER BY entry;
Query result
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:

SQLite
SELECT id, name || ' · ' || email AS contact
FROM customers
WHERE id IN (1, 4)
ORDER BY id;
A missing email turns the expression into NULL
idcontact
1Amy Wilson · amy@example.test
4NULL

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

Hard-to-read result
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.