| Author |
Topic |
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-07 : 02:39:47
|
| i am having two records in table as belowcol1 col2 col3221 test null221 test other221 test2 null221 test2 other218 Ascendingorder other218 Ascendingorder select864 1&2 3&4expected output:-col1 col2 col3221 test;test2 null;other218 Ascendingorder other;select864 1&2 3&4I want values col2 and col3 seperated with ';'for its col1 values ( i mean group by col1 ) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-07 : 06:54:15
|
| [code]Try this to eliminate amp for &declare @table table (col1 int,col2 varchar(66),col3 varchar(877))insert into @tableselect 221, 'test', 'null' union allselect 221, 'test', 'other' union allselect 221, 'test2', 'null' union allselect 221, 'test2', 'other' union allselect 218, 'Ascendingorder', 'other' union allselect 218, 'Ascendingorder', 'dff' union allselect 864, '1&2', '3&4' union allselect 864, '&asdf&' ,'asdf'select distinct col1,REPLACE(stuff((select distinct ';'+ col2 from @table where col1 = t.col1 for xml path('')),1,1,''),'&','&'),replace(stuff((select distinct ';'+ col3 from @table where col1 = t.col1 for xml path('')),1,1,''),'&','&') from @table t[/code]Jai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-09 : 01:13:01
|
| Hi jai thanks for response i used ur query , the output will differ from my expected output plz check my output, i want that format |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-09 : 01:24:26
|
| [code]hai jai,i got the solution declare @table table (col1 int,col2 varchar(66),col3 varchar(877))insert into @tableselect 221, 'test', 'null' union allselect 221, 'test', 'other' union allselect 221, 'test2', 'null' union allselect 221, 'test2', 'other' union allselect 218, 'Ascendingorder', 'other' union allselect 218, 'Ascendingorder', 'select' union allselect 864, '1&2', '3&4' select distinct col1,replace(stuff((select distinct ';'+ case when charindex('&',col2,1) >0 then replace(col2,'&',',') else col2 end from @table where col1 = t.col1 for xml path('')),1,1,''),',','&'),replace(stuff((select distinct ';'+ case when charindex('&',col3,1) >0 then replace(col3,'&',',') else col3 end from @table where col1 = t.col1 for xml path('')),1,1,''),',','&') from @table torder by col1[/code] |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-09 : 01:26:39
|
| My Solution too works Nageswar but it is being edited automaticallyJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-09 : 01:27:16
|
| Ok Ok Jai, Thanks any way for response |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-09 : 01:27:56
|
| Welcome NageswarJai Krishna |
 |
|
|
|
|
|