0

I have a table where one column may contain multiple values that I want to split and display individually by creating another row

So split Col2 and go from this:

| Col1 | Col2 |
| ---- | ---- |
| A    | red |
| B    | blue, green |
| C    | blue |
| D    | green, red, purple|
      

to this:

| Col1 | Col2 |
| ---- | ---- |
| A    | red |
| B    | blue|
| B    | green |
| C    | blue |
| D    | green|
| D    | red|
| D    | purple|

(T-SQL)

Dale K
  • 25,246
  • 15
  • 42
  • 71
tmcs10
  • 1
  • 1
    This has been [asked so many times](https://stackoverflow.com/search?q=%5Bsql-server%5D+split+column+to+rows+is%3Aquestion). Have a look at the results of that link. – squillman Oct 06 '21 at 19:03
  • Hi Dale - I've been trying to use the STRING_SPLIT() function but I'm not sure this can meet my needs as the data I want to split is already contained in a column in a table, thanks – tmcs10 Oct 06 '21 at 19:04
  • Good day and welcome to the stackoverflow forums. In addition to Dale's guidance, please provide queries to create the table and insert the sample data. You claimed that you have tables but you provided a description, which is not useful in order to reproduce the scenario. In the meantime, please search how to use the function `STRING_SPLIT` - this function do directly and exactly what you need. – Ronen Ariely Oct 06 '21 at 19:05
  • @tmcs10 Have a look at the question in the Closed header (blue box) above. I answered that one yesterday and it's the same solution you need for this situation. STRING_SPLIT with CROSS APPLY. – squillman Oct 06 '21 at 19:10
  • Thanks @squillman I was just looking at a function on [this answer](https://stackoverflow.com/questions/13527537/sql-query-to-split-column-data-into-rows) Thanks for the advice guys – tmcs10 Oct 06 '21 at 19:17
  • @tmcs10 That is WAY more complicated than you need. If you have STRING_SPLIT available to you then that is the way to go. – squillman Oct 06 '21 at 19:19

0 Answers0