search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

MySQL | ORD method

Database
chevron_right
MySQL
chevron_right
Documentation
chevron_right
Functions
chevron_right
String
schedule Mar 9, 2022
Last updated
local_offer MySQL
Tags

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
Ask a question or leave a feedback...