Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Column Value

Author  Topic 

sanjay5219
Posting Yak Master

240 Posts

Posted - 2013-01-25 : 08:52:37
Hi All,

I have a table T1 with column Namd and Hobbies
Name-Hobbies
Michael-Shopping
Michael-Movie
James-Shopping
James-Movie
James-Drawing

Now I have to create on column Hobbies1 and paste hobbies value like below
Name-Hobbies-Hobbies1
Michael-Shopping-Shopping,Movie
Michael-Movie-Shopping,Movie
James-Shopping-Shopping,Movie,Drawing
James-Movie-Shopping,Movie,Drawing
James-Drawing-Shopping,Movie,Drawing

Please suggest

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-01-25 : 09:02:56
[code]
DECLARE @t tABLE (Name varchar(10), Hobbies varchar(15))
insert into @t
SELECT 'Michael','Shopping' union all
SELECT 'Michael','Movie' union all
SELECT 'James','Shopping' union all
SELECT 'James','Movie' union all
SELECT 'James', 'Drawing'

SELECT name, Hobbies , STUFF(( SELECT ','+Hobbies FROM @t t2 WHERE t1.name= t2.Name FOR XML PATH('')), 1,1, '') AS Hobbies1
FROM @t t1[/code]

--
Chandu
Go to Top of Page

sanjay5219
Posting Yak Master

240 Posts

Posted - 2013-01-30 : 05:52:46
Thanks Chandu,

I have given just an example I am having around more than 10000 rows for this.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-30 : 05:54:42
quote:
Originally posted by sanjay5219

Thanks Chandu,

I have given just an example I am having around more than 10000 rows for this.


Thats not an issue
you can still apply the same method

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -