Click here to Skip to main content
15,743,541 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my SQL Server DB one row contains comma split values.
id | column_name
1 | 3,4,5,6,7

I want retrieve those values and store in to array.

please anyone suggest a way to accomplish this/ refer some sample code to do this.
It would be very helpful to me!

1 solution

SQL Server doesn't have a split function out-of-the-box so one easy way is to build your own and use it inside SQL statements. For example have a look at An Easy But Effective Way to Split a String using Transact-SQL[^]

If you don't want to use a function there are alternative approaches such as Split Comma Separated List Without Using a Function[^]

Of course one option is not to do it on the database side but just fetch the whole string value and then use String.Split[^] on the calling side to split the string and get an array.
Share this answer
CHill60 28-Sep-15 16:56pm    
5'd - especially for the split-it-on-the-calling-side bit.
Of course, one has to wonder why they've gone for a comma-separated column instead of a decent schema design ;-P
Wendelius 28-Sep-15 17:02pm    

Yep, it's awfully often I see different kinds of lists stored in a single column while all the fuzz could be avoided by adding a simple child table. I haven't yet figured out why such solutions are so tempting...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900