Trim String

  • How would I go about trimming the followin string, so that I only see the "File"? Everything after the last / will grow or shrink in characters. Any help will be appreciated.

    /Folder/File/Name

  • declare

    @STR nvarchar(max)

    ,@p1 int

    set @STR = '/Folder/File/Name'

    select @p1 = charindex('/', reverse(@str)) - 1

    select right(@str, @p1)

  • This will also work.

    SELECT PARSENAME(REPLACE('/Folder/File/Name', '/', '.'), 1)


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Try this

    SELECT PARSENAME(REPLACE('/Folder/File/Name', '/', '.'), 2)

  • Sony Francis @EY (6/14/2012)


    Try this

    SELECT PARSENAME(REPLACE('/Folder/File/Name', '/', '.'), 2)

    Looks familiar! 😉


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Worked perfect! Thanks for all the help.

Viewing 6 posts - 1 through 5 (of 5 total)

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