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
349 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now