MariaDB ASCII() 函数使用指南

在 MariaDB 中,ASCII() 是一个内置的字符串函数,它返回由参数指定的字符串参数的第一个字符的 ASCII 值。

MariaDB ASCII() 语法

这里是 MariaDB ASCII() 函数的语法:

ASCII(str)

参数

str
必需的。 一个需要返回 ASCII 值的字符。 如果多余一个字符,它将只返回第一个字符的 ASCII 值。

返回值

MariaDB ASCII() 函数返回给定的字符串的第一个字符的 ASCII 值。

如果参数 str 为空字符串 ''ASCII() 函数将返回 0

如果参数 strNULLASCII() 函数将返回 NULL

如果您需要返回更多字符的数字代码,请使用 ORD() 函数。

MariaDB ASCII() 示例

获取一个字符的 ASCII 值

这个语句使用 MariaDB ASCII() 函数获取字母 A 的 ASCII 值:

SELECT ASCII('A');

输出:

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

这告诉我们大写字母 A 的 ASCII 值是 65

获取字符串第一个字符的 ASCII 值

这个语句使用 MariaDB ASCII() 函数获取字符串 Hello 的第一个字母 H 的 ASCII 值:

SELECT ASCII('Hello');

输出:

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

获取所有大写字母的 ASCII 值

这个语句使用 MariaDB ASCII() 函数获取所有大写字母的 ASCII 值:

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";

输出:

+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 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 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

获取所有小写字母的 ASCII 值

这个语句使用 MariaDB ASCII() 函数获取所有小写字母的 ASCII 值:

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";

输出:

+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 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 值为 0

这个语句使用 MariaDB ASCII() 函数获取空字符串 '' 的 ASCII 值:

SELECT ASCII('');

输出:

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

参数为 NULL 时返回 NULL

这个语句使用 MariaDB ASCII() 函数并使用一个 NULL 参数:

SELECT ASCII(NULL);

输出:

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

结论

MariaDB ASCII() 函数返回由参数指定的字符串参数的第一个字符的 ASCII 值。