Concatenate texts from multiple rows in SQL

Today in this article, we will see how to Concatenate texts from multiple rows in SQL.

It’s pretty simple to combine multiple rows in SQL and create Concatenate texts using the STRING_AGG function.

Let’s take an example below. Here I have a sample table with the Bank code and Name of the employee matching the bank code.

Example

Concatenate texts from multiple rows in SQL

Expected output

Here we will try to get out as below,

FC100   X, Y, Z
FC345   A, P, S

What is STRING_AGG

  • The aggregate function STRING AGG concatenates every expression from every row into a single string.

  • The implicit conversion of expression values to string types is done before concatenation.

  • The implicit conversion to strings adheres to the standards for converting data types.

Command

STRING_AGG ( expression, separator ) [ <order_clause> ] <order_clause> ::= WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )

Example:

STRING AGG

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *