This In-line Table Valued Function takes a string of values along with a delimiter and breaks the string into a table using recursion. Support has been added for text qualified strings as well as space and tab delimited records.
Using a numbers table, we are able to take advantage of Recursion in SQL Server to find our delimiter and leverage the SUBSTRING function to break the string into its individual components without requiring the overhead of a loop.
Once the function has been created, splitting strings is quite simple.
DECLARE @String NVARCHAR(MAX) , @Delimiter NVARCHAR(3); SELECT @String = '"The","Quick","Brown","Fox","Jumped","Over","The","Lazy","Dog","0123456789","`~!@#$%^&*()_-+={}[]|\:;<,>.?/"' , @Delimiter = '","'; SELECT s.Record , s.Value , @String AS [@String] FROM dbo.udf_SplitString(@String,@Delimiter) AS s;
This script will produce the following results.
Happy Scripting!