search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

MySQL | ORD method

schedule Aug 11, 2023
Last updated
local_offer
MySQL
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

MySQL's ORD(~) method returns the character code (numeric value) for the leftmost character of the argument.

NOTE

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 representation C3A7. (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.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!