Quantcast
Channel: Concatenate text from multiple rows into a single text string - Stack Overflow
Browsing index pages (104 articles)
↧

Concatenate text from multiple rows into a single text string

Consider a database table holding names, with three rows:PeterPaulMaryIs there an easy way to turn this into a single string of Peter, Paul, Mary?

View Article


Answer by Manu for Concatenate text from multiple rows into a single text string

One way you could do it in SQL Server would be to return the table content as XML (for XML raw), convert the result to a string and then replace the tags with ", ".

View Article


Answer by Darryl Hein for Concatenate text from multiple rows into a single...

In MySQL, there is a function, GROUP_CONCAT(), which allows you to concatenate the values from multiple rows. Example:SELECT 1 AS a, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ') AS people FROM...

View Article

Answer by Dana for Concatenate text from multiple rows into a single text string

I don't have access to a SQL Server at home, so I'm guess at the syntax here, but it's more or less:DECLARE @names VARCHAR(500)SELECT @names = @names +''+ NameFROM Names

View Article

Answer by Chris Shaffer for Concatenate text from multiple rows into a single...

This answer may return unexpected results For consistent results, use one of the FOR XML PATH methods detailed in other answers.Use COALESCE:DECLARE @Names VARCHAR(8000) SELECT @Names = COALESCE(@Names...

View Article


Answer by gbn for Concatenate text from multiple rows into a single text string

DECLARE @Names VARCHAR(8000)SELECT @name = ''SELECT @Names = @Names +','+ Names FROM PeopleSELECT SUBSTRING(2, @Names, 7998)This puts the stray comma at the beginning.However, if you need other...

View Article

Answer by Ritesh for Concatenate text from multiple rows into a single text...

If you are on SQL Server 2017 or Azure, see Mathieu Renda answer.I had a similar issue when I was trying to join two tables with one-to-many relationships. In SQL 2005 I found that XML PATH method can...

View Article

Answer by teamchong for Concatenate text from multiple rows into a single...

In SQL Server 2005SELECT Stuff( (SELECT N', '+ Name FROM Names FOR XML PATH(''),TYPE) .value('text()[1]','nvarchar(max)'),1,2,N'')In SQL Server 2016you can use the FOR JSON syntaxi.e. SELECT...

View Article


Answer by jens frandsen for Concatenate text from multiple rows into a single...

One method not yet shown via the XMLdata() command in SQL Server is:Assume a table called NameList with one column called FName,SELECT FName +', ' AS 'data()'FROM NameListFOR XML...

View Article


Answer by Diwakar for Concatenate text from multiple rows into a single text...

Using XML helped me in getting rows separated with commas. For the extra comma we can use the replace function of SQL Server. Instead of adding a comma, use of the AS 'data()' will concatenate the rows...

View Article

Answer by Hans Bluh for Concatenate text from multiple rows into a single...

Use this:ISNULL(SUBSTRING(REPLACE((select ',' FName as 'data()' from NameList for xml path('')), ' ,',', '), 2, 300), '') 'MyList'Where the "300" could be any width taking into account the maximum...

View Article

Answer by user762952 for Concatenate text from multiple rows into a single...

In Oracle, it is wm_concat. I believe this function is available in the 10g release and higher.

View Article

Answer by Vladimir Nesterovsky for Concatenate text from multiple rows into a...

I usually use select like this to concatenate strings in SQL Server:with lines as ( select row_number() over(order by id) id, -- id is a line id line -- line of text. from source -- line source),...

View Article


Answer by Yogesh Bhadauirya for Concatenate text from multiple rows into a...

In SQL Server 2005 and later, use the query below to concatenate the rows.DECLARE @t table( Id int, Name varchar(10))INSERT INTO @tSELECT 1,'a' UNION ALLSELECT 1,'b' UNION ALLSELECT 2,'c' UNION...

View Article

Answer by Pramod for Concatenate text from multiple rows into a single text...

If you want to deal with nulls you can do it by adding a where clause or add another COALESCE around the first one.DECLARE @Names VARCHAR(8000) SELECT @Names = COALESCE(COALESCE(@Names +', ', '') +...

View Article


Answer by Oleg Sakharov for Concatenate text from multiple rows into a single...

I really liked elegancy of Dana's answer and just wanted to make it complete.DECLARE @names VARCHAR(MAX)SET @names = ''SELECT @names = @names +', '+ Name FROM Names-- Deleting last two symbols (',...

View Article

Answer by Daniel Reis for Concatenate text from multiple rows into a single...

A ready-to-use solution, with no extra commas:select substring( (select ', '+Name AS 'data()' from Names for xml path('')) ,3, 255) as "MyList"An empty list will result in NULL value.Usually you will...

View Article


Answer by Alex for Concatenate text from multiple rows into a single text string

Oracle 11g Release 2 supports the LISTAGG function. Documentation here.COLUMN employees FORMAT A50SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employeesFROM empGROUP BY deptno;...

View Article

Answer by jmoreno for Concatenate text from multiple rows into a single text...

A recursive CTE solution was suggested, but no code was provided. The code below is an example of a recursive CTE.Note that although the results match the question, the data doesn't quite match the...

View Article

Answer by hgmnz for Concatenate text from multiple rows into a single text...

PostgreSQL arrays are awesome. Example:Create some test data:postgres=# \c testYou are now connected to database "test" as user "hgimenez".test=# create table names (name text);CREATE TABLEtest=#...

View Article
Browsing index pages (104 articles)


Latest Images