In mysql, ASCII and ORD functions are all functions that convert characters into ascii code values. Next I will introduce to you the ASCII and ORD usage in mysql.
1. ASCII (str1)
Returns the ASCII code value of the leftmost character of the str string. If str is a Null String, 0 is returned. If 'str' is NULL, return NULL
Example:
1.
| The Code is as follows: |
Copy code |
Mysql> select ascii ('Hi '); + ----- + | Ascii ('Hi') | + ----- + | 1, 104 | + ----- + 1 row in set
|
104 is the ASCII value of h
2. Output ASCII values of B and B
| The Code is as follows: |
Copy code |
Mysql> select ascii ('B') AS Lower_Case, ASCII ('B') AS Upper_Case; + ---- + | Lower_Case | Upper_Case | + ---- + | 98 | 66 | + ---- + 1 row in set |
3. Use ASCII functions in the where statement
Output Data whose ASCII value is less than 70 at the beginning of aut_name
| The Code is as follows: |
Copy code |
SELECT aut_name, ASCII (aut_name) as "ASCII value of 1st character" FROM author Where ascii (aut_name) <70; |
4. The output field does not contain any data without ASCII values.
| The Code is as follows: |
Copy code |
SELECT * FROM table_name where not column_to_check REGEXP '[A-Za-z0-9.,-]'; |
5. Use it with SUBSTRING to calculate the second ASCII value after the string
| The Code is as follows: |
Copy code |
Mysql> select ASCII (SUBSTRING ('database', 2, 1 )); + ------------ + | ASCII (SUBSTRING ('database', 2, 1) | + ------------ + | 97 | + ------------ + 1 row in set |
2. ord Functions
ORD (str)
If the leftmost character of a string 'str' is a multi-byte character
| The Code is as follows: |
Copy code |
(First byte ASCII code) * 256 + (second byte ASCII code) [* 256 + third byte ASCII code...] |
Returns the ASCII code value of a character to return the multi-byte code. If the leftmost character is not a multi-byte character. Returns the same value as that returned by the ASCII () function.
| The Code is as follows: |
Copy code |
1 mysql> select ORD ('2 ');
2-> 50 |