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
83 changes: 81 additions & 2 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,90 @@ TiDB 支持使用大部分 MySQL 5.7 中提供的[字符串函数](https://dev.m

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

返回最左字符的数值
`ASCII()` 函数用于获取输入的参数中最左字符的 ASCII 值。该参数可以为字符串或数字。

- 如果输入参数不为空,该函数返回参数中最左字符的 ASCII 值。
- 如果输入参数为空,该函数返回 0。
lilin90 marked this conversation as resolved.
Show resolved Hide resolved
- 如果输入参数为 `NULL`,该函数返回 `NULL`。

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

示例:

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

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

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

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

```sql
SELECT ASCII(23);

+-----------+
| ASCII(23) |
+-----------+
| 50 |
+-----------+
```

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

返回一个数的二进制值的字符串表示
`BIN()` 函数用于将输入的参数转换为其二进制值的字符串表示形式。该参数可以为字符串或数字。

- 如果输入参数为正数,该函数返回该参数的二进制值的字符串表示形式。
- 如果输入参数为负数,该函数会将该参数的绝对值转换为其二进制值,然后对二进制值的每位取反(`0` 变为 `1`,`1` 变为 `0`),最后加上 `1`。
- 如果输入参数为字符串,且该字符串中只包含数字,该函数将按照该数字返回结果。例如,`"123"` 与 `123` 的返回结果相同。
- 如果输入参数为字符串,且该字符串第一个字符不是数字(如 `"q123"`),该函数返回 `0`。
- 如果输入参数为字符串,且该字符串由数字和非数字组成,该函数将按照该参数中最前面连续的数字返回结果。例如,`“123q123”` 与 `123` 的返回结果相同。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“123q123”

这是中文引号,需要更新一下 "123q123"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- 如果输入参数为 `NULL`,该函数返回 `NULL`。

示例:

```sql
SELECT BIN(123);

+----------+
| BIN(123) |
+----------+
| 1111011 |
+----------+
```

```sql
SELECT BIN(-7);

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

```sql
SELECT BIN("123q123");

+----------------+
| BIN("123q123") |
+----------------+
| 1111011 |
+----------------+
```

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

Expand Down
Loading