Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
chrismarin
Starting Member
2 Posts |
Posted - 2012-08-10 : 09:05:28
|
I have the following data:KeyField,listField1,ListField2123,"122,345,321","2,3,1"124,"222,221","5,2"I need to convert it to:KeyField,ListItem1,ListItem2123,122,2123,345,3123,321,1124,222,5124,221,2I have managed to do just one column resulting in:KeyField,ListItem1,ListItem2123,122,"2,3,1"123,345,"2,3,1"123,321,"2,3,1"124,222,"5,2"124,221,"5,2"I need the ListField2 to be split out too...The above result was using a For Each loop:Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) Dim keyField As Integer = Row.KeyField Dim itemList As String = Row.ListField Dim delimiter As String = "," If Not (String.IsNullOrEmpty(itemList)) Then Dim inputListArray() As String = _ itemList.Split(New String() {delimiter}, _ StringSplitOptions.RemoveEmptyEntries) For Each item As String In inputListArray With Output0Buffer .AddRow() .KeyField = keyField .ListItem = item .ListItem2 = Row.ListField2 End With Next End If End SubMany thanks in advance,Chris |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
chrismarin
Starting Member
2 Posts |
Posted - 2012-08-10 : 11:33:39
|
Thanks for the reply,I have now managed to sort this with the below code:Dim keyField As Integer = Row.KeyField Dim itemList As String = Row.listField1 Dim itemList2 As String = Row.ListField2 Dim delimiter As String = "," If Not (String.IsNullOrEmpty(itemList)) Then Dim inputListArray() As String = itemList.Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries) Dim inputListArray2() As String = itemList2.Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries) Dim counter As Integer = 0 For Each item As String In inputListArray With OutputBuffer .AddRow() .KeyField = keyField .ListItem = item .ListItem2 = inputListArray2.GetValue(counter) counter = counter + 1 End With Next End If |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-11 : 10:46:53
|
quote: Originally posted by chrismarin Thanks for the reply,I have now managed to sort this with the below code:Dim keyField As Integer = Row.KeyField Dim itemList As String = Row.listField1 Dim itemList2 As String = Row.ListField2 Dim delimiter As String = "," If Not (String.IsNullOrEmpty(itemList)) Then Dim inputListArray() As String = itemList.Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries) Dim inputListArray2() As String = itemList2.Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries) Dim counter As Integer = 0 For Each item As String In inputListArray With OutputBuffer .AddRow() .KeyField = keyField .ListItem = item .ListItem2 = inputListArray2.GetValue(counter) counter = counter + 1 End With Next End If
the t-sql approach i suggested is set based whereas this looks iterative (loop based)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|