Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string-functions: add instructions for using ASCII() and BIN() #15481

Merged
Merged
64 changes: 60 additions & 4 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,69 @@ TiDB 支持使用大部分 MySQL 5.7 中提供的[字符串函数](https://dev.m

## 支持的函数

### [`ASCII()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_ascii)
### [`ASCII(str)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_ascii)
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved

返回最左字符的数值
`ASCII(str)` 函数用于获取给定字符串 `str` 中最左字符的 ASCII 值。

### [`BIN()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_bin)
- 如果 `str` 不为空,该函数返回字符串中最左字符的 ASCII 值。
- 如果 `str` 为空字符串,该函数返回 0。
- 如果 `str` 为 NULL,该函数返回 NULL。

返回一个数的二进制值的字符串表示
> **注意:**
>
> `ASCII(str)` 只能处理那些用 8 个二进制数字(即,单个字节)来表示的字符。

示例:

```sql
SELECT ASCII('A');

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

```sql
SELECT ASCII('TiDB');

+------------+
| ASCII('T') |
+------------+
| 84 |
+------------+
```

### [`BIN(N)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_bin)
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved

`BIN(N)` 函数用于将 `BIGINT` 类型的数字 `N` 转换为其二进制值的字符串表示形式。

- 如果 `N` 为正数,该函数返回 `N` 的二进制值的字符串表示形式。
- 如果 `N` 为负数,该函数会将 `N` 的绝对值转换为其二进制值,然后对二进制值的每位取反(`0` 变为 `1`,`1` 变为 `0`),最后加上 `1`。
- 如果 `N` 为 NULL,该函数返回 NULL。

示例:

```sql
SELECT BIN(10);

+---------+
| BIN(10) |
+---------+
| 1010 |
+---------+
```

```sql
SELECT BIN(-7);

+------------------------------------------------------------------+
| BIN(-7) |
+------------------------------------------------------------------+
| 1111111111111111111111111111111111111111111111111111111111111001 |
+------------------------------------------------------------------+
```

### [`BIT_LENGTH()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_bit-length)

Expand Down
Loading