If you want do it directly from SQL, try the following query:
SELECT SUBSTRING(
'E:\asdas\sdfae\qargqerg\eqrger.pdf',
(LEN('E:\asdas\sdfae\qargqerg\eqrger.pdf') - CHARINDEX('\', REVERSE('E:\asdas\sdfae\qargqerg\eqrger.pdf'), 0)) + 2,
LEN('E:\asdas\sdfae\qargqerg\eqrger.pdf'));
How this works:
- You want to only extract the last part of the path (i.e. filename), so you'll obviously need a substring function.
Substring works like this:
Substring(string, start_index, end_index)
, where:
-
string
is the full word that you want to get a part out of
-
start_index
is the 0-based position of the first letter of the new string
-
end_index
is the position of the last letter of the new string. So, if we have:
Substring('Get me some codez', 12, 4)
will return
code
.
The second parameter in the query above is trickier. That one will take the string, reverse it, and get the first position of
\
(that's because SQL Server doesn't have the
LastIndexOf
function)
The third parameter is the length of the return string (which is the full string's length, because, for instance you can have no backslashes)
Also, I suggest you read some documentation on string functions:
-
Substring[^]
-
CharIndex[^]
-
Reverse[^]