string_agg()

A function for concatenating values into a string

string_agg() is a system function concatenating for non-NULL values into a string, optionally using a delimiter.

string_agg() was added in PostgreSQL 9.0.

Usage

string_agg ( value text, delimiter text ) → text
string_agg ( value bytea, delimiter bytea ) → bytea

string_agg() is - as the name implies - an aggregate function, meaning the input will usually be specified as one or more column names.

If no delimiter is required, pass an empty string ('') as the second parameter.

To concatenate an arbitrary number of text values into a single string, see concat() and concat_ws().

Change history

Examples

Basic, contrived usage example for string_agg():

postgres=# SELECT string_agg(x || '/' || y, ', ')
             FROM (values ('foo', 'bar'),
                          ('bar', 'baz')
                  ) _(x, y);
    string_agg
------------------
 foo/bar, bar/baz
(1 row)

Categories

String manipulation, System function

See also

array_agg()