PostgreSQL: STRING_AGG() to Concatenate String Per Each Group (Like SQL Server STUFF())

Last week on my Facebook Page, I have received a couple message about what is a similar solution of SQL Server STUFF () in PostgreSQL.

Most of the Database Developers require to perform String Aggregation based on different group of records.

Since PostgreSQL 9.0, STRING_AGG(expression, delimiter) function is available to perform String Aggregation operation.
Using STRING_AGG(), We can concatenate strings using different type of delimiter symbols.

Example of STRING_AGG():

Create a sample Students table:

Insert few sample records:

Concatenate Students Name per each Student Grade and arrange by Grade wise row (Using STRING_AGG()):

The Result:

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of