October 11, 2008 at 5:26 pm
I think I've outsmarted myself. I decided to store child data in one column of parent table as follows
ID Column ChildDataColumn
1 Test1 1;12;
2 Test2 2;15;
I can return the following with a Split function -
ID ChildDataColumn
1 1
1 12
but how do I return the following as a join to the parent table? -
ID ChildDataColumn
1 1
1 12
2 2
2 15
So that I come up with following -
ID Column Childdatacolumn
1 test 1
1 test 12
2 test2 2
2 test2 15
Any help is appreciated.
October 11, 2008 at 7:55 pm
If you want to return a table's worth of splits, then you have to stop worrying about the rows and worry only about the column. 😀
I don't know if your split function uses a Tally table or not, but let's assume you don't know what I'm talking about. Please see the following article for what a Tally table is and how it's used to replace loops.
http://www.sqlservercentral.com/articles/TSQL/62867/
Ok... on to your problem... you need to split an entire table's worth of delimited items. Her's the test data and the solution...
--===== Create and populate a test table according to the requirements.
-- This is NOT part of the solution
CREATE TABLE #YourTable (ID INT PRIMARY KEY CLUSTERED, [Column] VARCHAR(10), ChildDataColumn VARCHAR(100))
INSERT INTO #YourTable
(ID,[Column],ChildDataColumn)
SELECT '1','Test1','1;12;' UNION ALL
SELECT '2','Test2','2;15;'
--===== Split or "Normalize" the whole table at once
SELECT yt.ID,
yt.[Column],
SUBSTRING(';'+yt.ChildDataColumn,t.N+1,CHARINDEX(';',yt.ChildDataColumn,t.N)-t.N) AS Value
FROM dbo.Tally t
CROSS JOIN #YourTable yt
WHERE t.N < LEN(';'+yt.ChildDataColumn)
AND SUBSTRING(';'+yt.ChildDataColumn,N,1) = ';'
Also, for future posts, you'll get better answers quicker if you follow the suggestions in the link in my signature below.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 12, 2008 at 11:02 am
Jeff,
Thank you for the help. I really appreciate it. Point taken for future posts.
Darryl Jenkins
October 12, 2008 at 8:56 pm
I appreciate your concern about future posts... but more immediately, are you all set with the current post?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 13, 2008 at 1:29 pm
Worked like a charm. Thanks again.
Darryl Jenkins
October 13, 2008 at 3:38 pm
You bet... thanks for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply