How to get the row values as comma separated by a query

Showing Answers 1 - 42 of 42 Answers

vinod.km

  • May 16th, 2008
 

What I understood from your question  is that you want  to comma seperate the values of a particular column for its  different rows. If this the question then query will be

DECLARE @SQL AS VARCHAR(500)

SELECT @SQL = ISNULL(@SQL,' ')+',' + ColumnName FROM TableName

SELECT @SQL


  Was this answer useful?  Yes

Mahesh

  • Aug 22nd, 2011
 

Used in 11G

Code
  1. SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees

  2. FROM   emp

  3. GROUP BY deptno;

  4. Other than 11G

  5. SELECT  wm_concat(AMT) AS AMT

  6. FROM   TEST

  Was this answer useful?  Yes

rajesh kumar

  • Sep 7th, 2011
 

select row1||',',row2||',' ,row3 from table name;

  Was this answer useful?  Yes

rajesh kumar

  • Sep 7th, 2011
 

select COLUMN1||',' , COLUMN2||',' ,COLUMN3||',' from table name

  Was this answer useful?  Yes

NANNE SAHEB CHINTHALACHERUVU

  • Oct 4th, 2011
 

Code
  1.  SQL>Select

  2.          substr(sys_connect_by_path(e.ename,','),2) AS Employee_names

  3.          FROM (

  4.                    SELECT ename,row_number() over(ORDER BY ename) i FROM emp

  5.                  ) e

  6.           WHERE connect_by_isleaf=1

  7.           connect BY i = prior i + 1

  8.           start WITH i = 1 ;

  9.  

  10.  

Raj

  • Nov 7th, 2011
 

Code
  1. SELECT DISTINCT X.id, (

  2. Substring((SELECT Val + ',' FROM txtTable Y WHERE Y.id = X.Id ORDER BY X.ID FOR XML PATH(''))

  3. ,0,

  4. LEN((SELECT Val + ',' FROM txtTable Y WHERE Y.id = X.Id ORDER BY X.ID FOR XML PATH(''))) ))

  5. FROM txtTable X


  Was this answer useful?  Yes

kranthi swaroop

  • Dec 3rd, 2011
 

Kindly elobrate your issue.

As per my concern you can use CONCAT or PIE ||

Example

Code
  1. SELECT CONCAT (ename,',') FROM emp;

  2.  

  3. SELECT ename||',' FROM emp;

  Was this answer useful?  Yes

surendra kumar

  • May 8th, 2016
 

DECLARE @SQL AS VARCHAR(Max)
SELECT @SQL = coalesce(@SQL +;,)+Column Nmae from Table Name
print @SQL

  Was this answer useful?  Yes

Bikesh Srivastava

  • Aug 5th, 2016
 

Use this code to seprate row value in single coulmn

Code
  1. // CREATE coloumn  with seprated COLUMN value.FROM row value

  2. SELECT DISTINCT p.[Account ID],p.[Contact ID],p.[Account Name],p.[Email],p.[Contact Role],

  3.   STUFF((SELECT DISTINCT , + p1.Role

  4.          FROM finalsheet p1

  5.          WHERE p.[Contact ID]= p1.[Contact ID]

  6.             FOR XML PATH(), TYPE

  7.             ).value(., NVARCHAR(MAX))

  8.         ,1,1,) RoleS

  9. FROM finalsheet p

  10. --group by p.[Account ID],p.[Contact ID],p.[Account Name],p.[Email],p.[Contact Role]

  Was this answer useful?  Yes

punkaj

  • May 24th, 2017
 

using LISTAGG(colname) within group(order by col)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions