Detailed description of ASCII and ORD functions in mysql

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.