Thej
Thej

Reputation: 375

Find Path of the file using substring

I have list of file path stored in a column of a table. Now I need to extract only till last '\' in file path (i.e like below result set) Example:

column_A
--------------
G:\REPORTS\DDMS\PCP0.txt
G:\REPORTS\DPS\DEFAU.pdf

Result

G:\REPORTS\DDMS\
G:\REPORTS\DPS\

Upvotes: 0

Views: 83

Answers (3)

StackUser
StackUser

Reputation: 5398

Try this

DECLARE @str VARCHAR(500)='G:\REPORTS\DDMS\PCP0.txt'

SELECT LEFT(@str, len(@str) - CHARINDEX('\', REVERSE(@str))) 

Upvotes: 0

Randy Castleberry
Randy Castleberry

Reputation: 56

If you are working with .net , http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx Path.GetFileName will parse that for you.

Upvotes: 0

Pரதீப்
Pரதீப்

Reputation: 93694

Try this.

DECLARE @str VARCHAR(500)='G:\REPORTS\DDMS\PCP0.txt'

SELECT Reverse(Substring(Reverse(@str), Charindex('\', Reverse(@str)), Len(@str))) 

Upvotes: 3

Related Questions