-
Oct 28th, 2003, 03:37 PM
#1
T-SQL - Stripping Path From FileName
If you have a field in your database that contains the fille path to a file, such as \\Server\SharePoint\Folder1\SubFolder1\SomeOtherFolder\FileName.EXT and have had the need to return just FileName.EXT should find this handy.
In the select statement, just use this:
Code:
RIGHT(fldFileName, CHARINDEX('\', REVERSE(fldFileName))-1)
Replacing fldFileName with your own field.
It works like this:
REVERSE(fldFileName))
reverses the string so that it is backwards
CHARINDEX('\', REVERSE(fldFileName))
takes that reversed string, and finds the first '\'.... wich will be the LAST '\' when normal. Take one off of that (because we don't want the '\', and use that to get the RIGHT characters of the "proper" string.
It seems kind of wierd, but it does work. Far easier than parsing it out in VB or even in SQL.
TG
-
Sep 28th, 2012, 01:52 PM
#2
New Member
Re: T-SQL - Stripping Path From FileName
Simple, yet brilliant. Thanks, this helped tremendously.
-
Nov 8th, 2012, 08:16 PM
#3
New Member
Re: T-SQL - Stripping Path From FileName
cool tip - I made a slight change:
RIGHT('\'+fldFileName, CHARINDEX('\', REVERSE('\'+fldFileName))-1)
This way it will always work whether the '\' is in the filename or not (the original errors if it isn't)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|