Microsoft
Software
Hardware
Network
Question : I need to combine multiple columns to a single field in a comma delimited format in SQL 2005
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
Answer : I need to combine multiple columns to a single field in a comma delimited format in SQL 2005
this should generate the fields for you..
select 'ISNULL(' + column_name + ','',''),' from information_schema.columns
where table_name = 'tablename'
Random Solutions
GetDriveGeometry() Gives Wrong Information
Launch Acrobat Distiller from an Access 2002 database form
Email blast? 300+ recipients....where to start?
Crystal Reports - Replace certain text from database
Moving Ruyby on Rails website from one server to another server
DTSToDate Syntax Error?
logformat to get full URL
PHP Calling MS SQL Stored Procedures
ActiveSync working, OMA notand giving either runtime error/Security Exception error when accessed.
Continuous subform using a DAO recordset/ Access 2000