MySQL | ORD method
Start your free 7-days trial now!
MySQL's ORD(~)
method returns the character code (numeric value) for the leftmost character of the argument.
If the leftmost character is a multibyte character the following formula is used:
(1st byte code) + (2nd byte code * 256) + (3rd byte code * 256^2) + ...
Parameters
1. str
| string
The string whose leftmost character we should return the character code for.
Return value
The character code for the leftmost character of the input string.
Examples
Single byte character
To return the character code for leftmost character of 'David'
:
SELECT ORD('David');
+--------------+| ORD('David') |+--------------+| 68 |+--------------+
Note that this will return the same value as ASCII(~)
method.
Multi-byte character
To return the character code for leftmost character of 'ça va'
:
SELECT ORD('ça va');
+---------------+| ORD('ça va') |+---------------+| 50087 |+---------------+
Here, note the following:
ç
is a 2 byte character with hexadecimal representationC3A7
. (A7
in hexadecimal is equivalent to 167 in decimal,C3
in hexadecimal is equivalent to 195 in decimal.)As discussed earlier, the calculation for multibyte characters follows (1st byte code) + (2nd byte code * 256) and so on. In this case we get 167 + (195 * 256) which does indeed give us 50087.