How to use the MySQL ASCII() function

The ASCII() function in MySQL returns the ASCII code value of the leftmost character of a string. It can be used to get the ASCII value of alphanumeric and other characters.

Posted on

The ASCII() function in MySQL returns the ASCII code value of the leftmost character of a string. It can be used to get the ASCII value of alphanumeric and other characters.

Syntax

The syntax for ASCII() is:

ASCII(string)

Where string is the string to get the ASCII value from.

Examples

  1. Get ASCII value of a letter:

    SELECT ASCII('A');
    

    This returns 65, which is the ASCII code for uppercase letter ‘A’.

  2. Get ASCII value of a number:

    SELECT ASCII('4');
    

    This returns 52, the ASCII code for character ‘4’.

  3. Get ASCII code for a special character:

    SELECT ASCII('$');
    

    This returns 36, the ASCII code for dollar symbol ‘$’.

  4. Get ASCII value from a string:

    SELECT ASCII('Hello');
    

    This returns 72, the ASCII value for ‘H’, the leftmost character.

  5. Get ASCII value of a lowercase letter:

    SELECT ASCII('q');
    

    This returns 113, the ASCII code for lowercase ‘q’.

Other Similar Functions

Other ASCII-related functions in MySQL:

  • ORD() - Same as ASCII()
  • CHAR() - Return character for ASCII value
  • CONCAT() - Concatenate strings
  • INSERT() - Insert substring into string

So ASCII() provides a simple way to get the numeric ASCII code of characters in strings in MySQL.