November 14, 2007 at 11:00 am
I have a varchar(255) column that contains the fully qualified path for a file.
For example:
C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyFile.txt
I need to break this data into the path, filename and extension.
@Path = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP'
@Filename = 'MyFile.txt'
@Extension = 'txt'
I don't care if the path ends with a backslash or not.
Nor the filename containing the extension or not.
Nor the extension beginning with a period or not.
I can always strip/add them as necessary.
I'm thinking I need a loop to find the last backslash? Or some trick using REVERSE to find it? Any other ideas?
I'm not asking for code, just a point in the right direction in case I missed the obvious. Or some secret undocumented system stored procedure.
Thank you in advance for any assistance you can provide.
Joe
November 14, 2007 at 11:11 am
yeah a double reverse will work here; i broke it into a couple of steps to make it a bit more obvious:
declare @filename varchar(1000)
set @filename = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyFile.txt'
select
substring(reverse(@filename),1,charindex('\',reverse(@filename)) ), --txt.eliFyM\
reverse(substring(reverse(@filename),1,charindex('\',reverse(@filename)) ) ), --\MyFile.txt
reverse(substring(reverse(@filename),1,charindex('\',reverse(@filename)) -1 ) ), --MyFile.txt,
--returns: C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\
substring(@filename,1,charindex(reverse(substring(reverse(@filename),1,charindex('\',reverse(@filename)) ) ),@filename) )
Lowell
November 14, 2007 at 2:19 pm
Thats pretty slick Lowell. I did not know about REVERSE. I also got
SELECT SUBSTRING(@filename,1,LEN(@filename)-(CHARINDEX('\',REVERSE(@filename))-1)) AS Path
to work for path. I guess you learn something new everyday. Thanks for the lesson.
Greg
Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
November 14, 2007 at 3:17 pm
Thanks Lowell and Greg.
I really appreciate the quick feedback and sample code!
Sometimes I go down tangents and write a whole bunch of code only to find out that there is an undocumented system stored procedure or a simple technique.
This time I thought I'd check with the experts first.
Thanks again,
Joe
November 14, 2007 at 3:57 pm
Joe Barbian (11/14/2007)
This time I thought I'd check with the experts first.Thanks again,
Joe
Joe
I just want to make one thing perfectly clear. I am not to be included with the experts :). My knowledge of SQL is only the tip of the iceberg of what is there to know. I just enjoy working on others problems where I can, because it is the best opportunity I have to learn new stuff.
Greg
Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
November 14, 2007 at 6:45 pm
Greg Snidow (11/14/2007)
I just want to make one thing perfectly clear. I am not to be included with the experts :). My knowledge of SQL is only the tip of the iceberg of what is there to know. I just enjoy working on others problems where I can, because it is the best opportunity I have to learn new stuff.
With as much as SQL Server has expanded, I don't think anyone can be considered an expert on SQL Server as a whole any longer. π
K. Brian Kelley
@kbriankelley
September 28, 2015 at 3:34 am
SELECT RIGHT([FileName],CHARINDEX('\',REVERSE([FileName]),-1 )) AS NameOfFile
September 28, 2015 at 3:52 am
Path:
SELECT substring(filename,1,charindex(reverse(substring(reverse(filename),1,charindex('\',reverse(filename)) ) ),filename) )
FileName:
SELECT RIGHT([FileName],CHARINDEX('\',REVERSE([FileName]),-1 )) AS NameOfFile FROM SSIS_LogFileNameRecord
September 28, 2015 at 4:51 am
Anil Kolla (9/28/2015)
Path:SELECT substring(filename,1,charindex(reverse(substring(reverse(filename),1,charindex('\',reverse(filename)) ) ),filename) )
FileName:
SELECT RIGHT([FileName],CHARINDEX('\',REVERSE([FileName]),-1 )) AS NameOfFile FROM SSIS_LogFileNameRecord
Good effort, though it did take eight years longer than Lowell :hehe:
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 28, 2015 at 12:12 pm
Anil Kolla (9/28/2015)
SELECT RIGHT([FileName],CHARINDEX('\',REVERSE([FileName]),-1 )) AS NameOfFile
'Zactly.... no expensive double reverse required. One is bad enough. π No length calculation required, either.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 17, 2017 at 7:55 am
Query is almost correct.
I corrected and now it works for me
RIGHT([physical_device_name],CHARINDEX('\',REVERSE([physical_device_name]))-1) AS NameOfFile
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply