Trying to migrate from an old database to a new one in which the way data is stored is a bit different.
In one specific case I have a column with semicolon separated values that I would like to separate into multiple rows.
Here is an example:
SELECT
p.idperson,
p.roleperson
FROM person p
The TSQL above generates the following output
idperson roleperson
1001 ;214401;
1002 ;214201;214401;
1003 ;212101;
I would like to convert this to:
idperson roleperson
1001 214401
1002 214401
1002 214201
1003 212101
That is, I want to split the row with multiple values into two rows. Is this possible without creating cursors or loops?
While writing this post I figured out an easy way to do this using cross apply and a custom split function. So instead of just asking the question I’ll post how I solved it using this self contained SQL too 😉
After running the above, this is what SQL outputs.