Tuesday, March 06, 2012

SQL - Split Using Common Table Expression

In working with SQL server, I have often needed to use a Split function, which will take a string and a delimiter, and return a table of tokens from the string. The only problem is that my clients have not always been happy about needing to add a function to the DB.

I have recently discovered Common Table Expressions (CTEs), thanks to the San Diego Tech Immersion Group. I found a CTE that I was able to modify to produce the table of tokens from a string, just like the function above. Here's my modified query with a test string:
DECLARE @str AS NVARCHAR(MAX)
DECLARE @separator AS NVARCHAR(2)

SET @str = 'test1
test2
test3
test4'
SET @separator = CHAR(13) + CHAR(10)

;
WITH Split_CTE(rowNum, stringStart, stringEnd) AS
(
SELECT
1 AS rowNum,
CAST(1 AS BIGINT) AS stringStart,
CHARINDEX(@separator, @str) AS stringEnd
UNION ALL
SELECT
rowNum + 1 AS rowNum,
stringEnd + LEN(@separator) AS stringStart,
CHARINDEX(@separator, @str, stringEnd + 1) AS stringEnd
FROM Split_CTE
WHERE stringEnd > 0
)
SELECT
rowNum,
SUBSTRING(@str, stringStart,
CASE WHEN stringEnd > 0 THEN stringEnd - stringStart ELSE 2000000000 END) AS StringValue
FROM Split_CTE
;
I've tested this extensively for almost 2 minutes!

But seriously, if it fails when testing the application I'm working on now, I'll modify this post.