Remove Characters before Character Index

  • I have a column of data that has this /Product_Images/small/1/10032.png , I tried using RIGHT with CHARINDEX and was unable to get the results i was looking for.

    Can anyone help me just retrieve the everything after the last / so i get just the image.

    Best Regards,

    Cory

  • This might help.

    SELECT string, RIGHT( string, CHARINDEX( '/', REVERSE(string)) - 1)

    FROM (VALUES('/Product_Images/small/1/10032.png'))x(string)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • So my code is something like this in a long select statement

    select RIGHT([Items(Webex)].[image], CHARINDEX( '/', REVERSE([Items(Webex)].[image])) - 1) AS ImageName from PTS_Data

    I tried this and it returned an error

    Invalid length parameter passed to the RIGHT function.

  • I forgot to include the safety net. 😉

    SELECT string, RIGHT( string, CHARINDEX( '/', REVERSE('/' + string)) - 1)

    FROM (VALUES('/Product_Images/small/1/10032.png'), ('JustImage.png'))x(string)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thats perfect, I didnt think of using reverse. Thanks a million.

    Best Regards,

    Cory

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

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