Skip to content
Advertisement

MySQL Convert alphabet to number (e.g ‘A’ to 1, ‘B’ to 2…)

I’m making mysql SP, which has an input of type VARCHAR(20).

What I want to do is to convert the last character of input string to a number.

However last character can be number or letter, so if it is number, converting is not needed.

For example, input ‘APPLE’ will be converted to ‘5’ because ‘E’ is 5 in alphabetical order, ‘123’ will be ‘3’, and ‘BANANA’ will be ‘1’.

To do this, I splitted last character but don’t know how to convert.

I think that it would be possible with CASE ~ When, but is there any simpler way??

Advertisement

Answer

You can convert to a number using ASCII(). The logic you describe seems to be:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement