I figured out how to split a string based on delimiters (in this case spaces).
select parsename(replace(replace(replace([Column 0],' ',' '),' ',' '),' ','.'), 4) [Date],
parsename(replace(replace(replace([Column 0],' ',' '),' ',' '),' ','.'), 3) ID,
parsename(replace(replace(replace([Column 0],' ',' '),' ',' '),' ','.'), 2) Rank1,
parsename(replace(replace(replace([Column 0],' ',' '),' ',' '),' ','.'), 1) Rank2
--,replace(replace(replace(strCol,' ',' '),' ',' '),' ','.')
from AllData
Before.
After.
The thing is, I need to split strings, based on 1 to 7 delimiters, and I think the code above will become too hard to maintain at some point. Is there a more elegant script that can help me achieve what I want to do? Maybe a Table Value Function would be better. I'm not good at creating those things.
Thanks to all.
I am running SQL Server 2008.
Drop Table
[Raw_Data_ParsedIDs]
SELECT DISTINCT
split.a.value ('/A[1]', 'VARCHAR(MAX)') [Piece1],
split.a.value ('/A[2]', 'VARCHAR(MAX)') [Piece2],
split.a.value ('/A[3]', 'VARCHAR(MAX)') [Piece3],
split.a.value ('/A[4]', 'VARCHAR(MAX)') [Piece4],
split.a.value ('/A[5]', 'VARCHAR(MAX)') [Piece5],
split.a.value ('/A[6]', 'VARCHAR(MAX)') [Piece6],
split.a.value ('/A[7]', 'VARCHAR(MAX)') [Piece7]
INTO [Raw_Data_ParsedIDs]
FROM
(
SELECT CAST('<A>' + REPLACE(SrcID, '|', '</A><A>') + '</A>' AS XML) AS Data
FROM dbo.RAW_DATA_HIST
) a cross apply Data.nodes('/A') AS split(a)

