Quantcast
Channel: How to concatenate text from multiple rows into a single text string in SQL Server - Stack Overflow
Browsing latest articles
Browse All 49 View Live

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 Article



Image may be NSFW.
Clik here to view.

Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer by Hamid Bahmanabady for How to concatenate text from multiple rows...

declare @phone varchar(max)='' select @phone=@phone + mobileno +',' from members select @phone

View Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

How 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

Browsing latest articles
Browse All 49 View Live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>