Extract filename from fully qualified path

  • 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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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.

  • 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

  • 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.

  • 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

  • SELECT RIGHT([FileName],CHARINDEX('\',REVERSE([FileName]),-1 )) AS NameOfFile

  • 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

  • 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:

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    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

  • 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


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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