I am using the following function to split a string into rows. It is much faster than the previous function that I was using, however I need to somehow churn through this data quicker (its an ETL job):
ALTER FUNCTION [dbo].[ArrayToTable]
(
@InputString VARCHAR(MAX) = ''
, @Delimitter VARCHAR(1) = ','
)
RETURNS @RESULT TABLE([Position] INT IDENTITY, [Value] VARCHAR(MAX))
AS
BEGIN
DECLARE @XML XML
SELECT @XML = CONVERT(XML, SQL_TEXT)
FROM (
SELECT '<root><item>'
+ REPLACE(@InputString, @Delimitter, '</item><item>')
+ '</item></root>' AS SQL_TEXT
) dt
INSERT INTO @RESULT([Value])
SELECT t.col.query('.').value('.', 'VARCHAR(1000)') AS [Value]
FROM @XML.nodes('root/item') t(col)
RETURN
END
Can anyone think of a better/quicker way to turn a delimited string into rows? I am using a cross apply
on my query to join to these results.
Can anyone think of a more efficient method to turn delimited strings into rows?