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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 get filename from string
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/08/2013 :  09:09:08  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3873 Posts

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

madhivanan
Premature Yak Congratulator

India
22864 Posts

Posted - 10/08/2013 :  09:29:04  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 10/08/2013 :  09:47:28  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

1433 Posts

Posted - 10/08/2013 :  10:15:02  Show Profile  Reply with Quote
Thank you all.
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
352 Posts

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

djj
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22864 Posts

Posted - 10/08/2013 :  13:36:06  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000