Skip to main content
Concatenates non-NULL string values from a group into a single string, with each value separated by the specified delimiter.

Syntax

Parameters

ParameterDescriptionSupported input types
<expression>The string expression to aggregate.TEXT
<delimiter>The string placed between each pair of concatenated values.TEXT

Return Type

TEXT

Remarks

  • NULL values in <expression> are silently skipped.
  • If all input values are NULL, or if the input set is empty, STRING_AGG returns NULL.
  • No specific order for the concatenation of values is guaranteed. To concatenate in a specific order, use ARRAY_AGG, ARRAY_SORT, and ARRAY_TO_STRING.
  • An empty <delimiter> (empty string '') concatenates values without any separator.

Example

The levels table contains the following values:
level int nullname text nullleveltype text null
1Thunderbolt CircuitFastestLap
2Velocity ValeFirstToComplete
3Raceway RidgeFastestLap
4Nitro NarrowsFirstToComplete
5Thunder RoadFirstToComplete
6Burnout BoulevardDrift
7Speed StreetFastestLap
8Racing RavineFastestLap
9Drift DistrictDrift
10Acceleration AlleyFirstToComplete

Rows: 10Execution time: 5.13ms

The following example concatenates all level names within each leveltype, separated by commas:
leveltype text nulllevels text null
DriftBurnout Boulevard, Drift District
FastestLapThunderbolt Circuit, Raceway Ridge, Speed Street, Racing Ravine
FirstToCompleteVelocity Vale, Nitro Narrows, Thunder Road, Acceleration Alley

Rows: 3Execution time: 10.18ms

The following example shows that NULL values are skipped:
result text null
a,b,c

Rows: 1Execution time: 3.82ms