Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 get filename from string

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2013-10-08 : 09:09:08
Hello,
From the following string, how do I get the name of the file including the extension i.e. .csv
\\servername\foldername\filename_20131001.csv

So to get: "filename_20131001.csv"

Thank you

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-08 : 09:19:40
[code]DECLARE @x VARCHAR(255) = '\\servername\foldername\filename_20131001.csv'
SELECT REVERSE(LEFT(REVERSE(@x),CHARINDEX('\',REVERSE(@x)+'\')-1));[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-10-08 : 09:29:04
or

DECLARE @x VARCHAR(255) = '\\servername\foldername\filename_20131001.csv'
SELECT RIGHT((@x),CHARINDEX('\',reverse(@x))-1);


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-08 : 09:47:28
quote:
Originally posted by madhivanan

or

DECLARE @x VARCHAR(255) = '\\servername\foldername\filename_20131001.csv'
SELECT RIGHT((@x),CHARINDEX('\',reverse(@x))-1);


Madhivanan

Failing to plan is Planning to fail

Shorter and sweeter, Madhivanan!

Adding a "+'\'" in case there are records with no path:
RIGHT((@x),CHARINDEX('\',reverse(@x)+'\')-1)
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2013-10-08 : 10:15:02
Thank you all.
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-10-08 : 10:18:59
James, I hope I can remember the '\' tip! Would have never thought of it.

djj
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-10-08 : 13:36:06
quote:
Originally posted by James K

quote:
Originally posted by madhivanan

or

DECLARE @x VARCHAR(255) = '\\servername\foldername\filename_20131001.csv'
SELECT RIGHT((@x),CHARINDEX('\',reverse(@x))-1);


Madhivanan

Failing to plan is Planning to fail

Shorter and sweeter, Madhivanan!

Adding a "+'\'" in case there are records with no path:
RIGHT((@x),CHARINDEX('\',reverse(@x)+'\')-1)



Yes. Thanks

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -