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
354 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 57
SQL Improvement  ( Speed) 14 33
T-SQL: New to using transactions 9 47
2 IIF's in Access query 25 44
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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