Skip to content

Instantly share code, notes, and snippets.

@sgissinger
Created February 25, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgissinger/1e1d7573ef302439ed6677279970fe23 to your computer and use it in GitHub Desktop.
Save sgissinger/1e1d7573ef302439ed6677279970fe23 to your computer and use it in GitHub Desktop.
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