?
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
Medium Priority
?
365 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 600 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 600 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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