Click here to Skip to main content
15,885,887 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have 4 rows in a table with fields name,model,country.
in each row only the country differes. When i display these rows i must be able to show them in a single row with country being show with comma separated,like this.

Name Model Country
---------------------------------------------------------------------------
ABC XYZ India,US,UK


Please help me out
Posted

SQL
select [Name],Model,substring(Country,0,Len(country)) as Country from
    (
        select distinct [Name],Model,
        (select Country + ',' as [text()] from tabel where [Name]=a.[Name] and Model=a.Model for xml path('')) as Country
        from tabel as a
    )
as a

Happy Coding!
:)
 
Share this answer
 
v3
Comments
vangapally Naveen Kumar 11-Sep-12 5:41am    
Good Answer
Aarti Meswania 11-Sep-12 5:46am    
Thank you! :)
sunandandutt 11-Sep-12 5:44am    
My 5!
Aarti Meswania 11-Sep-12 5:45am    
thank you!
:)
Mohamed Mitwalli 11-Sep-12 5:46am    
5+
SQL
DECLARE @combinedString VARCHAR(MAX)
SELECT @combinedString = COALESCE(@combinedString + ', ', '') + Country
FROM MyTable

SELECT DISTINCT [Name],Model, @combinedString AS Country 
FROM MyTable
 
Share this answer
 

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