none
How to Retrive the file name from given file path?

    Question

  • The file path:

    "C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\Full Text  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc"

    I want to retrive  only file name (i.e.Crank Arm and Tire Maintenance.doc) from above file path

    Monday, May 20, 2013 7:09 AM

Answers

  • Try the below:

    Declare @s Varchar(1000) ='C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\FullText  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc'
    Select Substring(@s,(Len(@s)-CharIndex('\',REVERSE(@s)))+2,LEN(@s))


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, May 20, 2013 7:24 AM
  • DECLARE @path varchar(260) = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\Full Text  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc'
    
    SELECT RIGHT(@path,
                 CHARINDEX('\', REVERSE(@path)) - 1) AS FileName


    Olaf Helper

    Blog Xing

    Monday, May 20, 2013 7:39 AM

All replies

  • Try the below:

    Declare @s Varchar(1000) ='C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\FullText  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc'
    Select Substring(@s,(Len(@s)-CharIndex('\',REVERSE(@s)))+2,LEN(@s))


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, May 20, 2013 7:24 AM
  • The file path:

    "C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\Full Text  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc"

    I want to retrive  only file name (i.e.Crank Arm and Tire Maintenance.doc) from above file path

    Monday, May 20, 2013 7:31 AM
  • Check the below link you asked before.

    http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/47eac709-06cd-4f1f-bfb9-1d7aaa719d42


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, May 20, 2013 7:37 AM
  • declare @str nvarchar(500) = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\Full Text  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc'

    SELECT substring(@str, len(@str) - charindex('\', reverse(@str)) + 2, len(@str))


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
    Monday, May 20, 2013 7:37 AM
  • DECLARE @path varchar(260) = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\Full Text  Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc'
    
    SELECT RIGHT(@path,
                 CHARINDEX('\', REVERSE(@path)) - 1) AS FileName


    Olaf Helper

    Blog Xing

    Monday, May 20, 2013 7:39 AM
  • Try This

    DECLARE @v AS VARCHAR(2000) = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\Samples\Microsoft SQL Server Engine Samples\Full Text Search\ItemFinder\Documents\Crank Arm and Tire Maintenance.doc'
    SELECT REVERSE(LEFT(REVERSE(@v), CHARINDEX('\', REVERSE(@v)) - 1))

    Monday, May 20, 2013 8:00 AM
  • Hi Srujan,

    I would have used Right(string,len) function

    Thanks and Regards

    Santhu

    Monday, May 20, 2013 3:06 PM