I have a text field and I need to extract the last number in that text field.

The field will be a mix of numbers and letters but I only need the last number.

hfrh4hkvn65ndfon5093456nv - should return 6
ujsvo3453ojkn3124ono5onhjgc should return 5

  • Where exactly you need this - Apex/ JavaScript/ Formula field?
    – Sneh
    May 6, 2022 at 13:33
  • Sorry should have mentioned it is a formula field.
    – Joe Hayes
    May 6, 2022 at 13:39
  • Vote for this idea: ideas.salesforce.com/s/idea/a0B8W00000Gdh0UUAR/… . Some suggestions in the comments under this idea could be used to solve your case if the length of your string is 1) fixed 2) small enough to write out the formula for each character position. and not hit the max chars formula limit.
    – identigral
    May 6, 2022 at 15:47
  • are there constraints on the characters in the string - for example - only lower case letters plus numbers? If so, you could use repeated SUBSTITUTE expressions to replace a with NULL, b with NULL, etc. and then you are left with only numbers - SFDC recently made the formula limit 15000 compiled chars so my approach might work for constrained cases
    – cropredy
    May 6, 2022 at 17:46

1 Answer 1


There's no function that does this easily in formulas. This is one of those cases where you'll want to use Apex to implement your business logic.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .