Solved

I need to combine multiple columns to a single field in a comma delimited format in SQL 2005

Posted on 2008-10-02
10
357 Views
Last Modified: 2012-05-05
Here is a prt of my table.  The Syms columns go up to 47 with Syms48 always null and there are about 1400 ChunkIDs.

ChunkID      Syms1      Syms2      Syms3      Syms4      Syms5
1522      Scarlatina      Scarlet fever      NULL      NULL      NULL
1524      Placenta previa      NULL      NULL      NULL      NULL
1532      Barrett's esophagus      NULL      NULL      NULL      NULL
1536      Martin-Bell syndrome      NULL      NULL      NULL      NULL
1543      Respiratory syncytial virus      NULL      NULL      NULL      NULL
11460      Candidiasis      Vaginal yeast infection      Yeast infection      NULL      NULL
11461      Wrist sprain      NULL      NULL      NULL      NULL
11462      Wrist fracture      NULL      NULL      NULL      NULL
11463      Pertussis      Whooping cough      NULL      NULL      NULL
11464      Whiplash      NULL      NULL      NULL      NULL
11465      Farsightedness      Hyperopia      Myopia      Nearsightedness      NULL
11467      Viral sore throat      Viral sore throat (Viral pharyngitis)      NULL      NULL      NULL
11469      Aseptic meningitis      Viral meningitis      NULL      NULL      NULL
11471      Vertigo      NULL      NULL      NULL      NULL
11474      Varicose veins      NULL      NULL      NULL      NULL
11476      Urinary tract infection (UTI)      NULL      NULL      NULL      NULL
11477      Pelvic floor hernia      Pudendal hernia      Uterine prolapse      NULL      NULL
11479      Endometrial cancer      Uterine cancer      NULL      NULL      NULL

What I need to do with this is take all the Syms colums for each ID and put them a comma delimited list in a single column called Syms.  So I need it to look like this.

ChunkID      Syms
1522      Scarlatina,Scarlet fever
1524      Placenta previa
1532      Barrett's esophagus
1536      Martin-Bell syndrome
1543      Respiratory syncytial virus
11460      Candidiasis,Vaginal yeast infection,Yeast infection
11461      Wrist sprain
11462      Wrist fracture
11463      Pertussis,Whooping cough
11464      Whiplash
11465      Farsightedness,Hyperopia      Myopia,Nearsightedness
11467      Viral sore throat,Viral sore throat (Viral pharyngitis)
11469      Aseptic meningitis,Viral meningitis
11471      Vertigo
11474      Varicose veins
11476      Urinary tract infection (UTI)
11477      Pelvic floor hernia,Pudendal hernia,Uterine prolapse
11479      Endometrial cancer,Uterine cancer

It would be great if it could be done dynamicly because of the number of columns in this table and the fact that not all ChunkIDs have the same number of Syms.  I bet this is simple bit it is over my beginner head.

Thanks in Advance for any and all help,
JasonM
0
Comment
Question by:JasonM1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22626157
Something like this will work:

;with CommaData as(select ChunkID,
isnull(Syms1+',','')+isnull(Syms2+',','')+isnull(Syms3+',','')+isnull(Syms4+',','')+isnull(Syms5+',','') cd
from YourTable)
select chunkID, left(cd,len(cd)-1)
from CommaData
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22626166
you can use SSIS to do this...right click the DB, go to all tasks, then export.  In wizard, specify your table name as the source and a flat file as the destination.  From there, it will create a .csv file for you.
0
 

Author Comment

by:JasonM1981
ID: 22626251
BrandonGalderisi

Your solution creates the dataset I'm looking for, however the Syms columns go up to Syms47.  Is there anyway to dynamicly get those columns in there so I do not have to type out +isnull(Syms5+',','')+isnull(Syms6+',','')+isnull(Syms7+',','') over and over again.  Thanks for the help so far.

JasonM
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 60

Expert Comment

by:chapmandew
ID: 22626281
..and hopefully none of the Syms columns are integers...  :)
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 150 total points
ID: 22626290
this should generate the fields for you..

select 'ISNULL(' + column_name + ','',''),' from information_schema.columns
where table_name = 'tablename'
0
 

Author Comment

by:JasonM1981
ID: 22626292
None of them are int...they are all text.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22626326
I'm working on something cooler maybe.... i have to bounce something off of chap first.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22626332
cooler as in a cooler way to do this for you.
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 150 total points
ID: 22626689
Ok...

This is a co-creation of myself and chapmandew... I took it so far, he finished it off.

Let us know if you have any questions.

create table #t (chunkid int, sym1 varchar(32), sym2 varchar(32), sym3 varchar(32))
insert into #t values(1,'scar', 'bear',null)
insert into #t values(2,'scar', 'bear','tom')
insert into #t values(300,'scar', 'bear','tom')
 
go
declare @x xml
set @x=(select * from #t
for xml auto,root('data'))
 
declare @i int
exec sp_xml_preparedocument @i=@i output, @x=@x
 
;with xmlTable as
(select * From openxml(@i,'/data',1) t)
--select * from xmltable
 
select xr1.id, xr2.localname as ChunkID,cast(xr3.text as varchar(50)) as valfield
into #temp2
from XMLTable xr1
join xmltable xr2
  on xr2.parentid = xr1.id
join xmltable xr3
  on xr3.parentid = xr2.id
where xr1.nodetype=1
and xr2.nodetype=2
 
select valfield, 
reverse(substring(reverse((
select valfield + ',' 
from #temp2 i
where o.id = i.id and chunkid <> 'chunkid'
for xml path('')
)), 2, 3000))
from #temp2 o
where chunkid = 'chunkid'
 
 
exec sp_xml_removedocument @i=@i
go
drop table #t
drop table #temp2

Open in new window

0
 

Author Closing Comment

by:JasonM1981
ID: 31502458
Thanks all for the help.  That is a very cool way to do it.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with Progress 4gl Rounding Function 6 60
Data conversion error in SSIS 3 55
SQL Server how to create a DYNAMIC TABLE? 11 51
T-SQL: Need to trim a single leading space 7 62
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question