Do you have a comment or suggestion to improve SQL Server? We’d love to hear it!

Support DISTINCT for STRING_AGG

Currently STRING_AGG aggregates all strings passed as an input. It would be very useful to support DISTINCT, so it would concatenate unique strings only.

Example:
CREATE TABLE dbo.Test (a VARCHAR(255))
INSERT INTO dbo.Test (a)
VALUES('Str1')
,('Str2')
,('Str1')

SELECT STRING_AGG(a, ',') FROM dbo.Test t

It returns: 'Str1,Str2,Str1'

SELECT STRING_AGG(DISTINCT a, ',') FROM dbo.Test t

It would return: 'Str1,Str2'

54 votes
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)

    We’ll send you updates on this idea

    Alexander Sharovarov shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    6 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base