Get substring between "\" where multiple "\"

Posted by AceAlfred on Stack Overflow See other posts from Stack Overflow or by AceAlfred
Published on 2013-11-06T17:21:56Z Indexed on 2013/11/07 9:54 UTC
Read the original article Hit count: 199

Filed under:
|
|
|
|

Found this solution to get substring after slash () character

DECLARE @st1 varchar(10)
SET @st1 = 'MYTEST\aftercompare'
SELECT @st1
,SUBSTRING(@st1, CHARINDEX('\', @st1) + 1, LEN(@st1))

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5c3a5e2c-54fc-43dd-b12c-1a1f6784d7d8/tsql-get-substring-after-slash-character

But is there a way to get substring after second slash or even more?

DECLARE @st1 varchar(50)
--Added more slashes
SET @st1 = 'MYTEST\aftercompare\slash2\slash3\slash4'
SELECT @st1
--This part would need some work
--,SUBSTRING(@st1, CHARINDEX('\', @st1) + 1, LEN(@st1))

And getting only the substring between the slashes.

Values: [1] "aftercompare" - [2] "slash2" - [3] "slash3" - [4] "slash4"

© Stack Overflow or respective owner

Related posts about sql

Related posts about tsql