Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursive CTE
DECLARE @str varchar(100) = '1721,1603,1063,1683,2049'
DECLARE @delimiter varchar(10) = ','
;
WITH mycte AS
(
SELECT 0 a, 1 b
UNION ALL
SELECT b, CHARINDEX(@delimiter, @str, b) + LEN(@delimiter)
FROM mycte
WHERE b > a
)
SELECT SUBSTRING(@str, a,
CASE WHEN b > LEN(@delimiter)
THEN b - a - LEN(@delimiter)
ELSE LEN(@str) - a + 1 END) value
FROM mycte WHERE a > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.