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
344 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

18 Experts available now in Live!

Get 1:1 Help Now