Answer by Ammad Amir for How to concatenate text from multiple rows into a...
In SQL Server 2017 or later versions, you can concatenate text from multiple rows into a single text string in SQL Server using the STRING_AGG function. Here's an example of how you can achieve...
View ArticleAnswer by sameer Ahmed for How to concatenate text from multiple rows into a...
In SQL Server 2017 or later versions, you can use the STRING_AGG() function to generate comma-separated values. Please have a look below at one example.SELECT VendorId, STRING_AGG(FirstName,',')...
View ArticleAnswer by panser for How to concatenate text from multiple rows into a single...
In PostgreSQL - array_aggSELECT array_to_string(array_agg(DISTINCT rolname), ',') FROM pg_catalog.pg_roles;Or STRING_AGGSELECT STRING_AGG(rolname::text,',') FROM pg_catalog.pg_roles;
View ArticleAnswer by Amirreza mohammadi for How to concatenate text from multiple rows...
First of all you should declare a table variable and fill it with your table data and after that, with a WHILE loop, select row one by one and add its value to a nvarchar(max) variable. Go declare...
View ArticleAnswer by Arash.Zandi for How to concatenate text from multiple rows into a...
This worked for me (SQL Server 2016):SELECT CarNamesString = STUFF(( SELECT ','+ [Name] FROM tbl_cars FOR XML PATH('') ), 1, 1, '')Here is the source: https://www.mytecbits.com/For newer SQL versions...
View ArticleAnswer by asmgx for How to concatenate text from multiple rows into a single...
On top of Chris Shaffer's answer:If your data may get repeated, such asTomAliJohnAliTomMikeInstead of having Tom,Ali,John,Ali,Tom,MikeYou can use DISTINCT to avoid duplicates and get...
View ArticleAnswer by Kemal AL GAZZAH for How to concatenate text from multiple rows into...
We can use RECUSRSIVITY, WITH CTE, union ALL as followsdeclare @mytable as table(id int identity(1,1), str nvarchar(100))insert into @mytable values('Peter'),('Paul'),('Mary')declare @myresult as...
View ArticleAnswer by Esperento57 for How to concatenate text from multiple rows into a...
With a recursive query you can do it:-- Create example tableCREATE TABLE tmptable (NAME VARCHAR(30)) ;-- Insert example dataINSERT INTO tmptable VALUES('PETER');INSERT INTO tmptable...
View ArticleAnswer by Ravi Pipaliya for How to concatenate text from multiple rows into a...
Here is the complete solution to achieve this:-- Table CreationCREATE TABLE Tbl( CustomerCode VARCHAR(50), CustomerName VARCHAR(50), Type VARCHAR(50),Items VARCHAR(50))insert into TblSELECT...
View ArticleAnswer by Pooja Bhat for How to concatenate text from multiple rows into a...
Below is a simple PL/SQL procedure to implement the given scenario using "basic loop" and "rownum"Table definitionCREATE TABLE "NAMES" ("NAME" VARCHAR2(10 BYTE))) ;Let's insert values into this...
View ArticleAnswer by Max Szczurek for How to concatenate text from multiple rows into a...
SELECT STUFF((SELECT ', '+ name FROM [table] FOR XML PATH('')), 1, 2, '')Here's a sample:DECLARE @t TABLE (name VARCHAR(10))INSERT INTO @t VALUES ('Peter'), ('Paul'), ('Mary')SELECT STUFF((SELECT ', '+...
View ArticleAnswer by Shahbaz for How to concatenate text from multiple rows into a...
Although it's too late, and already has many solutions. Here is simple solution for MySQL:SELECT t1.id, GROUP_CONCAT(t1.id) ids FROM table t1 JOIN table t2 ON (t1.id = t2.id) GROUP BY t1.id
View ArticleAnswer by Mathieu Renda for How to concatenate text from multiple rows into a...
SQL Server 2017+ and SQL Azure: STRING_AGGStarting with the next version of SQL Server, we can finally concatenate across rows without having to resort to any variable or XML witchery.STRING_AGG...
View ArticleAnswer by Henrik Fransas for How to concatenate text from multiple rows into...
In SQL Server vNext this will be built in with the STRING_AGG function. Read more about it in STRING_AGG (Transact-SQL).
View ArticleAnswer by Tigerjz32 for How to concatenate text from multiple rows into a...
You need to create a variable that will hold your final result and select into it, like so.Easiest SolutionDECLARE @char VARCHAR(MAX);SELECT @char = COALESCE(@char +', '+ [column], [column]) FROM...
View ArticleAnswer by Glen for How to concatenate text from multiple rows into a single...
Not that I have done any analysis on performance as my list had less than 10 items but I was amazed after looking through the 30 odd answers I still had a twist on a similar answer already given...
View ArticleAnswer by Graeme for How to concatenate text from multiple rows into a single...
SQL Server 2005 or laterCREATE TABLE dbo.Students( StudentId INT , Name VARCHAR(50) , CONSTRAINT PK_Students PRIMARY KEY (StudentId));CREATE TABLE dbo.Subjects( SubjectId INT , Name VARCHAR(50) ,...
View ArticleAnswer by Muhammad Bilal for How to concatenate text from multiple rows into...
SELECT PageContent = Stuff( ( SELECT PageContent FROM dbo.InfoGuide WHERE CategoryId = @CategoryId AND SubCategoryId = @SubCategoryId for xml path(''), type ).value('.[1]','nvarchar(max)'), 1, 1,...
View ArticleAnswer by Mike Barlow - BarDev for How to concatenate text from multiple rows...
With the other answers, the person reading the answer must be aware of a specific domain table such as vehicle or student. The table must be created and populated with data to test a solution.Below is...
View ArticleAnswer by Pedram for How to concatenate text from multiple rows into a single...
Use COALESCE - Learn more from hereFor an example:102103104Then write the below code in SQL Server,Declare @Numbers AS Nvarchar(MAX) -- It must not be MAX if you have few numbersSELECT @Numbers =...
View ArticleAnswer by user1767754 for How to concatenate text from multiple rows into a...
MySQL complete example:We have users who can have much data and we want to have an output, where we can see all users' data in a list:Result:___________________________| id | rowList...
View ArticleAnswer by Nizam for How to concatenate text from multiple rows into a single...
This answer will require some privilege on the server to work.Assemblies are a good option for you. There are a lot of sites that explain how to create it. The one I think is very well explained is...
View ArticleAnswer by Hamid Bahmanabady for How to concatenate text from multiple rows...
declare @phone varchar(max)='' select @phone=@phone + mobileno +',' from members select @phone
View ArticleAnswer by Rapunzo for How to concatenate text from multiple rows into a...
To avoid null values you can use CONCAT()DECLARE @names VARCHAR(500)SELECT @names = CONCAT(@names, '', name) FROM Namesselect @names
View ArticleAnswer by Max Tkachenko for How to concatenate text from multiple rows into a...
With the 'TABLE' type it is extremely easy. Let's imagine that your table is called Students and it has column name.declare @rowsCount INTdeclare @i INT = 1declare @names varchar(max) = ''DECLARE...
View ArticleAnswer by topchef for How to concatenate text from multiple rows into a...
This method applies to the Teradata Aster database only as it uses its NPATH function.Again, we have table StudentsSubjectID StudentName---------- -------------1 Mary1 John1 Sam2 Alaina2 EdwardThen...
View ArticleAnswer by endo64 for How to concatenate text from multiple rows into a single...
This can be useful toocreate table #test (id int,name varchar(10))--use separate inserts on older versions of SQL Serverinsert into #test values (1,'Peter'), (1,'Paul'), (1,'Mary'), (2,'Alex'),...
View ArticleAnswer by ZeroK for How to concatenate text from multiple rows into a single...
For Oracle DBs, see this question: How can multiple rows be concatenated into one in Oracle without creating a stored procedure?The best answer appears to be by @Emmanuel, using the built-in LISTAGG()...
View ArticleAnswer by Priti Getkewar Joshi for How to concatenate text from multiple rows...
There are a couple of ways in Oracle: create table name (first_name varchar2(30)); insert into name values ('Peter'); insert into name values ('Paul'); insert into name values ('Mary');Solution is 1:...
View ArticleAnswer by hgmnz for How to concatenate text from multiple rows into a single...
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 ArticleAnswer by jmoreno for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Alex for How to concatenate text from multiple rows into a single...
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 ArticleAnswer by Daniel Reis for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Oleg Sakharov for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Pramod for How to concatenate text from multiple rows into a single...
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 ArticleAnswer by Yogesh Bhadauirya for How to concatenate text from multiple rows...
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 ArticleAnswer by Vladimir Nesterovsky for How to concatenate text from multiple rows...
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 ArticleAnswer by user762952 for How to concatenate text from multiple rows into a...
In Oracle, it is wm_concat. I believe this function is available in the 10g release and higher.
View ArticleAnswer by Hans Bluh for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Diwakar for How to concatenate text from multiple rows into a...
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 ArticleAnswer by jens frandsen for How to concatenate text from multiple rows into a...
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 ArticleAnswer by teamchong for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Ritesh for How to concatenate text from multiple rows into a single...
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 ArticleAnswer by gbn for How to concatenate text from multiple rows into a single...
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 ArticleAnswer by Chris Shaffer for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Dana for How to concatenate text from multiple rows into a single...
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 ArticleAnswer by Darryl Hein for How to concatenate text from multiple rows into a...
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 ArticleAnswer by Manu for How to concatenate text from multiple rows into a single...
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 ArticleHow to concatenate text from multiple rows into a single text string in SQL...
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
More Pages to Explore .....