take part of a string

  • Hi,

    how do i take a part of a string that will show in this example only the

    test.mdf

    the string = "c:\test\backuptest\test\test.mdf"

    THX

  • Is the filename static and if not will you know what it is? Check out string functions in BOL too, there may be some inspiration there.

    Max

  • Thx.

    no the file name is not static but what static is the . and then 3 letters

    and before that there is this so we have in the end \test.mdf

    i need only the test.mdf

    THX

  • simplest way is to build a function to handle it

    function will need to loop backwards through the string to find the last "\"

    once you have that location you can work out the substring function you'll need to get the file name to return to the caller

  • can you help me with that?

  • check this post.

    http://www.sqlservercentral.com/Forums/Topic635219-146-1.aspx#bm635460

    It has all your answers.

    Regards,
    Nitin

  • this post do exactly the opposite.

    it remove the last string that i need.

    THX

  • Read whole post. It has your answer as well in posts.

    Regards,
    Nitin

  • Here is the solution...

    DECLARE @strFullPath VARCHAR(MAX)

    SELECT @strFullPath = 'E:\SomeFile.sql'

    SELECT RIGHT( @strFullPath, CHARINDEX( '\', REVERSE( @strFullPath ) ) - 1 )

    --Ramesh


  • Mad-Dog (2/3/2009)


    Thx.

    no the file name is not static but what static is the . and then 3 letters

    and before that there is this so we have in the end \test.mdf

    i need only the test.mdf

    THX

    This code will be able to split out the File name after the last '\' and before the '.'

    DECLARE @FileName AS VARCHAR(100)

    SET @FileName = 'c:\test\backuptest\test\test.mdf'

    SELECT REVERSE(SUBSTRING(REVERSE(@FileName),CHARINDEX('.',reverse(@FileName))+1,CHARINDEX('\',reverse(@FileName))-(CHARINDEX('.',reverse(@FileName))+1)))

  • I hope you have read the solution on the post which I have given...

    If not then here is the solution

    DECLARE @s-2 VARCHAR(100)

    SET @s-2 = 'D:\backup\databasename\Backupname.bak'

    SELECT SUBSTRING(@s, LEN(@s) - CHARINDEX('\', REVERSE(@s)) + 2, LEN(@s))

    Regards,
    Nitin

  • Thanks to everyone.

    it working good.

  • Slight variation on the last post:

    DECLARE @STR nvarchar(100)

    SET

    @STR = 'c:\test\backuptest\test\test.mdf'

    SET

    @STR = REVERSE(@str)

    SELECT

    REVERSE(LEFT(@str, CHARINDEX('\', @STR) - 1))

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply