-1

There is no default split function in SQL Server 2012 that I'm using.

I want to split the string (ex: /Folder1/Folder2/) by /.

  • if string is /Folder1/ then output should be Folder1,
  • if string is /Folder1/Folder2/ then output should be Folder2,
  • if string is /Folder1/Folder2/Folder3/ then output should be Folder3.
5
  • What version of SQL server are you on? Commented Sep 30, 2018 at 9:24
  • @Reno Sql server 2012 Commented Sep 30, 2018 at 9:25
  • 1
    A combination of the REVERSE, CHARINDEX, and SUBSTRING functions will get you there. Commented Sep 30, 2018 at 9:29
  • This question has been answered before in many different flavours: stackoverflow.com/questions/2647/… Commented Sep 30, 2018 at 9:35
  • @TT I found the answer with your help SELECT REVERSE(SUBSTRING(SUBSTRING(REVERSE('/Folder1/Folder2/'),2,len(REVERSE('/Folder1/Folder2/'))),1,charIndex('/',SUBSTRING(REVERSE('/Folder1/Folder2/'),2,len(REVERSE('/Folder1/Folder2/'))))-1)) Commented Sep 30, 2018 at 9:43

1 Answer 1

1

Try this:

declare @tbl table (path varchar(100));
insert into @tbl values
('/Folder1/'),
('/Folder1/Folder2/'),
('/Folder1/Folder2/Folder3/');

select *, 
       replace(substring(path, len(path) - charindex('/', reverse(path), 2) + 1, 1000), '/', '')
from @tbl
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.