Find last index of character in string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    Find last index of character in string

    Hi, can anyone point me to a function in MSSQL that returns the last index of a character in a string? I'm trying to find the base filename from a field that returns the full path.

    Eg. if db contained rows:
    C:\temp\file1.b at
    C:\temp\Aug10\f ile2.bat

    I would want as result:
    file1.bat
    file2.bat


    The only solution I've come up with so far is to create a temporary table with the filenames, and recurse using update statements on the table until there are no more updates. There has to be a better solution.
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Use CHARINDEX() to find the last "\". Start the search from the right.

    Then use RIGHT() or SUBSTRING(). You might also need the LEN() function.

    Good luck

    -- CK

    Comment

    Working...