Click here to Skip to main content
15,906,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear sir,

I have following data. i need the data will be one line in sql server2008 Like

Input:

ID Name Marks
1 ABC 50
1 ABC 60
1 ABC 70

Output:
1 ABC 50,60,70
Posted

Hi,

you can use below query for result,

SQL
DECLARE @Marks VARCHAR(8000)
SELECT @Marks = COALESCE(@Marks + ', ', '') + Marks FROM tableName 
SELECT @Marks


Thanks
-Amit.
 
Share this answer
 
Comments
codeBegin 6-Jun-12 5:27am    
Exact one +5!
AmitGajjar 6-Jun-12 5:28am    
Thanks codeBegin.
COALESCE (Transact-SQL)[^]
COALESCE accepts one or more column names of the same data type. The COALESCE function checks the value of each column in the order in which they are listed and returns the first nonmissing value. If only one column is listed, the COALESCE function returns the value of that column. If all the values of all arguments are missing, the COALESCE function returns a missing value.

Coalesce Function[^]
Concatenate many rows into a single text string using SQL Server 2008[^]
Concatenate multiple rows into one column[^]
 
Share this answer
 
v2
Comments
AmitGajjar 6-Jun-12 5:08am    
Nice 5+
codeBegin 6-Jun-12 5:27am    
:) Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900