Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substring extraction

Status
Not open for further replies.
Sep 27, 2001
179
AU
Hi

I have a field that holds a filepath, e.g.

c:\program files\document.doc

What I would like to do is extraact the file name, i.e. document.doc

I thought I could use a combination of the right and patindex commands to extract this by checking for the first occurence of the backslash from the right, but patindex always reads left to right.

Could anyone offer a method to do this?

Thanks
 
SQL Server has a reverse function.

Code:
declare @Temp VarChar(100)
Set @Temp = 'c:\program files\document.doc'

Select Right(@Temp, CharIndex('\', Reverse(@Temp))-1)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I should mention that this code has issues when the '\' does not appear in the data. This was not meant to be a complete solution, merely a starting point.

Hope it helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top