I have a VARCHAR column that contains a bit of text (non-numeric) followed by an integer, e.g. 'foobar: 42'
. I would like to get the SUM() of the numbers, but I can't think of a way to do it.
- If the number was at the beginning, just summing the column would work, but apparently the content has to begin with the number. (It didn't throw an error, but I got 0 as a result.)
- If I was sure of the number of leading non-numerics I could use SUBSTRING(), but I can't be sure.
- I thought of using something like LEFT(Column, INSTR(Column, '4')), but I would have to check for any of the ten digits, and I don't see a way to do that.
- REGEXP doesn't appear to help me because it only does matching, not replacement.
Does someone have a clever idea? Yes, I can do a loop in PHP later if I have to, but there are other complexities involved that would get pretty messy if I had to do it that way.