1

I have a table that contains a column with comma separated values. I need to separate those values into new rows. Table looks like this :

ID|      DATE|Value|Feed|
 1|10-10-2014|5.00 |1,3,4
 2|10-11-2014|21.00|1
54|01-15-2015|8.24 |2,15
 1|02-22-2015|5.14 |1,3,4

And I need to break it out to :

ID|      DATE|Value|Feed|
 1|10-10-2014|5.00 |1
 1|10-10-2014|5.00 |3
 1|10-10-2014|5.00 |4
 2|10-11-2014|21.00|1
54|01-15-2015|8.24 |2
54|01-15-2015|8.24 |15
 1|02-22-2015|5.14 |1
 1|02-22-2015|5.14 |3
 1|02-22-2015|5.14 |4

So I believe I need a to write a table valued function but I'm having a difficult time figuring out where to start.

Any guidance would be great.

Thanks.

Empty Set
  • 41
  • 1
  • 3

1 Answers1

0

You are going to need the ID so that you can join back to your table, but here is some help for the Split function https://stackoverflow.com/a/10914602/3854195

EDIT: (How to use a cursor to combine the results from the Split function with your source table) I setup this SQL Fiddle

Community
  • 1
  • 1
Morpheus
  • 1,616
  • 1
  • 21
  • 31
  • The part I seem to be having issues with is that every example I've seen shows the function being called and passed a user string. I'm not sure how I can pass proper column into the function and have it correlate back to the proper ID? – Empty Set Sep 17 '15 at 16:12
  • Typically, cursors are frowned upon, but I'm not sure there is a better way. I'll setup an example and then update my answer later. – Morpheus Sep 17 '15 at 20:31
  • @EmptySet that was my first time using SQL Fiddle. Let me know if it works out for you. – Morpheus Sep 18 '15 at 20:13