Function Reference

CONCAT_WS

Joins strings, inserting the given separator between them.

PostgreSQL 17.5
CONCAT_WS(separator, str[, ...])

Parameters

separatortext
Separator inserted between the strings
str
String to join

Return value

A string; NULL arguments are skipped, but a NULL separator gives NULL

Examples

Date from parts with hyphens

PostgreSQL 17.5
SELECT CONCAT_WS('-', '2024', '03', '08');
concat_ws
2024-03-08

Full name from three columns

PostgreSQL 17.5
SELECT CONCAT_WS(' ', last_name, first_name, middle_name) AS full_name
FROM Teacher;
full_name
Romashkin Pavel Petrovich
Kolesnikov Vasilij Antonovich
Vaulina Mariya Stepanovna
ZHedrinskaya Taisiya Timurovna
Sosnovskaya Marina Gennadevna
Myasoedova Ekaterina YAkovlevna
Arakcheeva Liliya Timurovna
Krauze Sofya Dmitrievna
Ostrozhskaya Viktoriya Nikolaevna
Krylov YUrij

Details

Empty strings are not skipped, and the separator is placed around them too: CONCAT_WS('-', 'a', '', 'b') returns a--b.

See also