Author |
Topic |
dadster
Starting Member
2 Posts |
Posted - 2010-12-03 : 04:51:31
|
I am trying to fix some legacy data and need to parse data from a field to use to create new recordsI have:ID,Code,Data-------------1,aa/dd/ff/gg,some data 2,gg,more data 3,kk/ll/oo/pp,3535 and want to convert to:ID,Code,Data-------------1,aa,some data 1,dd,some data 1,ff,some data 1,gg,some data 2,gg,more data 3,kk,3535 3,ll,3535 3,oo,3535 3,pp,3535 IE Parse the string in the code field and create new records if neededI know I can do it with a cursor, but I'm struggling with a set based solution. |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-03 : 05:03:21
|
Something like;with cte as(select id, data, code, locstrt = 1, locend=charindex('/',code) from tblunion allselect id, data, code, locstrt = locend+1, locend=charindex('/',code,locend+1) from cte where locend<>0)select id, code=substring(code,locstrt,case when locend = 0 then len(code) else locend end - locstrt), datafrom cte==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
dadster
Starting Member
2 Posts |
Posted - 2010-12-03 : 05:15:30
|
Thanks nigelrivett - works perfectly. Haven't played much with CTE - I think I need to check it out! |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
|
|
|
|