MariaDB ASCII() Function

In MariaDB, ASCII() is a built-in string function that returns the ASCII value of the first character of the specified string argument.

MariaDB ASCII() Syntax

Here is the syntax of the MariaDB ASCII() function:

ASCII(str)

Parameters

str

Required. A character whose ASCII value needs to be returned. If there is more than one character, it will just return the ASCII value of the first character.

Return value

The MariaDB ASCII() function returns the ASCII value of the first character of the given string.

If the argument str is an empty string '', the ASCII() function will return 0.

If the argument str is NULL, the ASCII() function will return NULL.

If you need to return numeric codes for more characters, use the ORD() function.

MariaDB ASCII() Examples

Get the ASCII value of a character

This statement uses the MariaDB ASCII() function to get the ASCII value of the letter A:

SELECT ASCII('A');

Output:

+------------+
| ASCII('A') |
+------------+
|         65 |
+------------+

This tells us 65 is the ASCII value of the capital letter A.

Get the ASCII value of the first character of the string

This statement uses the MariaDB ASCII() function to get the ASCII value of the first letter H of the string Hello:

SELECT ASCII('Hello');

Output:

+----------------+
| ASCII('Hello') |
+----------------+
|             72 |
+----------------+

Get the ASCII value of all uppercase letters

This statement uses the MariaDB ASCII() function to get the ASCII value of all uppercase letters:

SELECT
    ASCII('A') AS "A",
    ASCII('B') AS "B",
    ASCII('C') AS "C",
    ASCII('D') AS "D",
    ASCII('E') AS "E",
    ASCII('F') AS "F",
    ASCII('G') AS "G",
    ASCII('H') AS "H",
    ASCII('I') AS "I",
    ASCII('J') AS "J",
    ASCII('K') AS "K",
    ASCII('L') AS "L",
    ASCII('M') AS "M",
    ASCII('N') AS "N",
    ASCII('O') AS "O",
    ASCII('P') AS "P",
    ASCII('Q') AS "Q",
    ASCII('R') AS "R",
    ASCII('S') AS "S",
    ASCII('T') AS "T",
    ASCII('U') AS "U",
    ASCII('V') AS "V",
    ASCII('W') AS "W",
    ASCII('X') AS "X",
    ASCII('Y') AS "Y",
    ASCII('Z') AS "Z";

Output:

+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| A  | B  | C  | D  | E  | F  | G  | H  | I  | J  | K  | L  | M  | N  | O  | P  | Q  | R  | S  | T  | U  | V  | W  | X  | Y  | Z  |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

Get the ASCII value of all lowercase letters

This statement uses the MariaDB ASCII() function to get the ASCII value of all lowercase letters:

SELECT
    ASCII('a') AS "a",
    ASCII('b') AS "b",
    ASCII('c') AS "c",
    ASCII('d') AS "d",
    ASCII('e') AS "e",
    ASCII('f') AS "f",
    ASCII('g') AS "g",
    ASCII('h') AS "h",
    ASCII('i') AS "i",
    ASCII('j') AS "j",
    ASCII('k') AS "k",
    ASCII('l') AS "l",
    ASCII('m') AS "m",
    ASCII('n') AS "n",
    ASCII('o') AS "o",
    ASCII('p') AS "p",
    ASCII('q') AS "q",
    ASCII('r') AS "r",
    ASCII('s') AS "s",
    ASCII('t') AS "t",
    ASCII('u') AS "u",
    ASCII('v') AS "v",
    ASCII('w') AS "w",
    ASCII('x') AS "x",
    ASCII('y') AS "y",
    ASCII('z') AS "z";

Output:

+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| a  | b  | c  | d   | e   | f   | g   | h   | i   | j   | k   | l   | m   | n   | o   | p   | q   | r   | s   | t   | u   | v   | w   | x   | y   | z   |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 |
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

ASCII value of the empty string is 0

This statement uses the MariaDB ASCII() function to get the ASCII value of the empty string '':

SELECT ASCII('');

Output:

+-----------+
| ASCII('') |
+-----------+
|         0 |
+-----------+

Returns NULL if the parameter is NULL

This statement uses the MariaDB ASCII() function ands takes a NULL parameter:

SELECT ASCII(NULL);

Output:

+-------------+
| ASCII(NULL) |
+-------------+
|        NULL |
+-------------+

Conclusion

The MariaDB ASCII() function returns the ASCII value of the first character of the given string argument.