T-sql substring

I basically have one column that needs to be tweaked. The format of the column is as follows:

Column A -------- Root\FOLDERPATH\somesubfolderpath\somedocument.doc 

The first line of "Root \" will always be the same length with the same characters. Everything after β€œRoot” may vary depending on the folder structure of the document.

What I need to get is the second line. Using the above example, the column should look like lilke:

 Column A -------- FOLDERPATH 

So, I need to cut "Root", get the line after that and cut everything after that.

+4
source share
2 answers

One of the methods:

 substring(fld, 6, charindex('\', substring(fld + '\', 6, len(fld))) -1) 
+6
source
 declare @val varchar(100) set @val = 'Root\FOLDERPATH\somesubfolderpath\somedocument.doc' select left(right(@val, len(@val) - 5), charindex('\', right(@val, len(@val) - 5)) - 1) 
0
source

Source: https://habr.com/ru/post/1379333/


All Articles