I need to be able to take a column from a SQL table and write a query that will output this in a sentence format. example
column A
---------
Abraham
Jones
Henry
Walter
output would look like this
Abraham, Jones, Henry, Walter
I need to be able to take a column from a SQL table and write a query that will output this in a sentence format. example
column A
---------
Abraham
Jones
Henry
Walter
output would look like this
Abraham, Jones, Henry, Walter
If you are using SQL Server 2017 or above:
DECLARE @Table TABLE( ColumnName VARCHAR(25))
INSERT INTO @Table VALUES ('Abraham'),('Jones'),('Henry'),('Walter')
SELECT STRING_AGG(ColumnName,', ') sentence
FROM @Table
For SQL Server versions below 2017, use the For Xml Path approach:
SELECT STUFF((SELECT ', ' + ColumnName
FROM @Table t1
FOR XML PATH (''))
, 1, 1, '')
This Stackoverflow post explains more. Good luck!