SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Column Value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sanjay5219
Posting Yak Master

230 Posts

Posted - 01/25/2013 :  08:52:37  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/25/2013 :  09:02:56  Show Profile  Reply with Quote

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


--
Chandu
Go to Top of Page

sanjay5219
Posting Yak Master

230 Posts

Posted - 01/30/2013 :  05:52:46  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/30/2013 :  05:54:42  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000