| Author |
Topic |
|
sqlserverdeveloper
Posting Yak Master
243 Posts |
Posted - 2009-02-27 : 17:59:18
|
| I have a table with the following values:col1 col2emp1 aaemp1 bbemp1 ccemp1 ddemp2 qqemp2 wwemp2 eeI am trying to create third column by concatenating values of col2 with a comma as follows:col1 col2 col3emp1 aa aa,bb,cc,ddemp1 bb aa,bb,cc,ddemp1 cc aa,bb,cc,ddemp1 dd aa,bb,cc,ddemp2 qq qq,ww,eeemp2 ww qq,ww,eeemp2 ee qq,ww,eeplease advice how I can have col3 displayed in the final output data. Thx. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-27 : 23:45:28
|
| select col1 ,col2 ,stuff(( select ','+col2 from urtable where col1 = t.col1 for xml path('')),1,1,'') as col3 from urtable t |
 |
|
|
Mangal Pardeshi
Posting Yak Master
110 Posts |
Posted - 2009-03-02 : 01:24:42
|
Are you sure you want this waycol1 col2 col3emp1 aa aa,bb,cc,ddemp1 bb aa,bb,cc,ddemp1 cc aa,bb,cc,ddemp1 dd aa,bb,cc,ddemp2 qq qq,ww,eeemp2 ww qq,ww,eeemp2 ee qq,ww,eeI guess this one makes more senseCol1 AllCol2emp1 aa, bb, cc, ddemp2 qq, ww, eeTrySELECT Col1, AllCol2 = REPLACE((SELECT col2 as 'data()' FROM csv i WHERE I.COL1 = csv.col1 FOR XML PATH('')),' ',', ')FROM csv GROUP BY Col1http://mangalpardeshi.blogspot.com/2009/02/create-comma-delimited-list-from-column_888.htmlMangal Pardeshihttp://mangalpardeshi.blogspot.com |
 |
|
|
|
|
|