Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Posts
    2

    Post Unanswered: CHARINDEX starting from the end?

    Hi,
    I have a problem finding the last occurrence of a character in a string.
    The charindex seems just able to search from left to right..

    Anyone pleas give me a hand.

    /Memgard

  2. #2
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    see if this helps...

    Code:
    --------------------------------------------------------------------------------------------------
    declare @s varchar(17)
    set @s = '1234|67890A|CDEFG'
    select @s as 'Original String'
    select reverse(@s) as 'In Reverse', datalength(@s) as 'String Length'
    select charindex('|',reverse(@s)) as 'Location of first ''|'' in Reverse String'
    select datalength(@s) - charindex('|',reverse(@s)) + 1 as 'Location of last ''|'' in Original string'
    --------------------------------------------------------------------------------------------------
    Paul Young
    (Knowledge is power! Get some!)

  3. #3
    Join Date
    Jun 2002
    Posts
    2

    Thanks...

    That did help.
    Thanks a lot!

    Stange that SQL Server dosn't support this with a built in function.
    In Oracle you can just add a negative value to scan from the right to left...

  4. #4
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    I have found over the years that Oracle and SQL Server both get the job done, just in diffrent ways.
    Paul Young
    (Knowledge is power! Get some!)

  5. #5
    Join Date
    May 2012
    Posts
    1
    Sweet!
    SELECT RIGHT(ProcessedFilePath,(Charindex('\',Reverse(Pro cessedFilePath))-1)

    works for parsing the file name out of a long url. That just saved me a ton of time!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •