[Solved] How to extract a string between two of the SAME delimiters T-SQL?


Use CHAR_INDEX twice:

SELECT *, SUBSTRING(path, pos1 + 1, pos2 - pos1 - 1)
FROM tests
CROSS APPLY (SELECT NULLIF(CHARINDEX('\', path), 0)) AS ca1(pos1)
CROSS APPLY (SELECT NULLIF(CHARINDEX('\', path, pos1 + 1), 0)) AS ca2(pos2)

-- NULLIF is used to convert 0 value (character not found) to NULL

Test on db<>fiddle

1

solved How to extract a string between two of the SAME delimiters T-SQL?