aggregate
STRING_AGG
Combines non-NULL text values with a delimiter between them. Supply aggregate-local ordering for a reproducible sequence; duplicates remain. Empty input yields NULL. Example target: PostgreSQL 17; inline synthetic data; no extensions.
Formal Syntax & Structure
sql syntaxsql
STRING_AGG(value, delimiter ORDER BY sort_key)Working Example
Example Snippetsql
WITH sample(id,x,y) AS (VALUES (1,1,2),(2,2,4),(3,3,6),(4,4,8),(5,NULL,4)) SELECT string_agg(CAST(x AS text), ',' ORDER BY id) FROM sample;