You can contact multiple row values into a single text string value by using the SQL built-in function
STRING_AGG().
Note- STRING_AGG() function supported in SQL Server version 2017 and later
Syntax-
SELECT STRING_AGG(column_name, ', ') as ConcatedText FROM table_name
The STRING_AGG() function requires 2 argument(s), the first one is the
column name and the second one is a separater between values as you see in the above syntax.
Example-
suppose we have a Employees table.
CREATE TABLE Employees
(
EmpId INT PRIMARY KEY NOT NULL,
EmpName NVARCHAR(50),
Gender NVARCHAR(20),
Salary DECIMAL(10, 2)
)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
You can contact multiple row values into a single text string value by using the SQL built-in function
STRING_AGG().Syntax-
The
STRING_AGG()function requires 2 argument(s), the first one is the column name and the second one is a separater between values as you see in the above syntax.Example-
suppose we have a
Employeestable.Let's insert some values into above table,
As you can see in the below picture,
Example-
Now, use STRING_AGG() to concat multiple row values into a single string,
Also, Read: How to use searching and filtering data in an SQL server?