Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Extracting a number from a variable string

How can I extract the numbers from string with varying length and composition?


I.E.

"Triazolam 0.1" -> 0.1

"Estazolam 10" -> 10

"Neg Ctrl" -> 0

"Bkg" -> 0

"Clonazepam 500+" -> 500


Many thanks

Posted on Nov 20, 2018 3:33 PM

Reply
Question marked as Best reply

Posted on Nov 21, 2018 11:08 PM

If you have a string followed by a space and then a number you can do something like this:


User uploaded file


=RIGHT(A2,LEN(A2)−FIND(" ",A2,1))*1


That won't work when there are following non-number characters as in Clonazepam 500+. If there aren't too cases where you have trailing non-number characters you could strip them manually.


Substitute ; for , in the formula if your region uses , as a decimal separator.


SG

7 replies
Question marked as Best reply

Nov 21, 2018 11:08 PM in response to brandonzar

If you have a string followed by a space and then a number you can do something like this:


User uploaded file


=RIGHT(A2,LEN(A2)−FIND(" ",A2,1))*1


That won't work when there are following non-number characters as in Clonazepam 500+. If there aren't too cases where you have trailing non-number characters you could strip them manually.


Substitute ; for , in the formula if your region uses , as a decimal separator.


SG

Nov 21, 2018 11:08 PM in response to brandonzar

If you have a consistent pattern and in some cases a final + then you could "preprocess" in a separate column like this:'

User uploaded file


Formula in B2, filled down:


=IF(RIGHT(A2,1)="+",LEFT(A2,LEN(A2)−1),A2)


Formula in C2, filled down:


=IFERROR(RIGHT(B2,LEN(B2)−FIND(" ",B2,1))*1,0)



Or, if you like you could put it all in one long, ugly formula:


User uploaded file


In B2, filled down:


=IFERROR(RIGHT(IF(RIGHT(A2,1)="+",LEFT(A2,LEN(A2)−1),A2),LEN(IF(RIGHT(A2,1)="+", LEFT(A2,LEN(A2)−1),A2))−FIND(" ",IF(RIGHT(A2,1)="+",LEFT(A2,LEN(A2)−1),A2),1))*1,0)



SG

Nov 21, 2018 2:24 PM in response to brandonzar

If it is just one specific character (i.e., the "+") or a small set of characters that dangle at the end of the string after the number, You can first run the string through SUBSTITUTE, turning the +'s into null strings.


=SUBSTITUTE(A2,"+","")


or if you have two characters that might be at the end, say they are a "+" or a "-" (or both or multiples of both)

=SUBSTITUTE(SUBSTITUTE(A2,"+",""),"-","")


Then you can strip the text from the front as Barry showed to extract the number by itself.

Nov 21, 2018 10:51 PM in response to Badunit

"Then you can strip the text from the front as Barry showed to extract the number by itself."


Well, I had thought of that, and later, of using SUBSTITUTE, but it was SGIII that actually posted the first part, and you that posted the SUSTITUTE suggestion.


Wayne correctly And I think we all agree with SG's implied non-support of the "one long, ugly formula" single column solution provided.


Regards,

Barry

Extracting a number from a variable string

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.