Created
February 25, 2019 15:11
-
-
Save sgissinger/1e1d7573ef302439ed6677279970fe23 to your computer and use it in GitHub Desktop.
Recursive CTE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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