STRING_AGG Function in SQL Server(TSQL)

 

We can concatenate string columns with STRING_AGG function. STRING_AGG function requires 2 argument. Thats why we must put seperater between values.

Let’s make examples for a better understanding of STRING_AGG Function.

Example:

First, we create a table with the help of the following script and add a few records into this table.

If you execute the following query you will receive the below error.

Msg 174, Level 15, State 1, Line 9

The STRING_AGG function requires 2 argument(s).

STRING_AGG Function Usage:

It has two parameters. The first one is the name of your column that you want to concatenate. The second one is the seperator value. You can see how we can use this function below.

As you can see in the below results, null values didnt return as a result.

If you want you can replace null values with the below script:

If you want to concatenate more than one column you can use the below script:

dbtut
Author: dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

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