T-SQL: Opposite to string concatenation - how to split string into multiple records

Posted by kristof on Stack Overflow See other posts from Stack Overflow or by kristof
Published on 2008-11-24T17:17:56Z Indexed on 2010/05/14 21:14 UTC
Read the original article Hit count: 405

Filed under:
|
|

I have seen a couple of questions related to string concatenation in SQL. I wonder how would you approach the opposite problem: splitting coma delimited string into rows of data:

Lets say I have tables:

userTypedTags(userID,commaSeparatedTags) 'one entry per user
tags(tagID,name)

And want to insert data into table

userTag(userID,tagID) 'multiple entries per user

Inspired by Which tags are not in the database? question

EDIT

Thanks for the answers, actually more then one deserves to be accepted but I can only pick one, and the solution presented by Cade Roux with recursions seems pretty clean to me. It works on SQL Server 2005 and above.

For earlier version of SQL Server the solution provided by miies can be used. For working with text data type wcm answer will be helpful. Thanks again.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql-server