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

function-and-operators: enrich instructions for string functions BIT_LENGTH() and CHAR() #15482 #16516

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,104 @@ SELECT BIN(-7);

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

返回字符串的位长度
`BIT_LENGTH()` 函数用于返回输入参数的长度(以比特为单位)。
qiancai marked this conversation as resolved.
Show resolved Hide resolved

示例:

```sql
SELECT BIT_LENGTH("TiDB");

+--------------------+
| BIT_LENGTH("TiDB") |
+--------------------+
| 32 |
+--------------------+
```

每个字符 8 位 x 4 个字符 = 32 位

```sql
SELECT BIT_LENGTH("PingCAP 123");

+---------------------------+
| BIT_LENGTH("PingCAP 123") |
+---------------------------+
| 88 |
+---------------------------+
```

每个字符 8 位(空格也会被计算在内,因为它是非字母数字字符) x 11 个字符 = 88 位

```sql
SELECT CustomerName, BIT_LENGTH(CustomerName) AS BitLengthOfName FROM Customers;

+--------------------+-----------------+
| CustomerName | BitLengthOfName |
+--------------------+-----------------+
| Albert Einstein | 120 |
| Robert Oppenheimer | 144 |
+--------------------+-----------------+
```

> **注意:**
>
> 上面这个示例假设数据库中存在一个名为 `Customers` 的表,表中有一个名为 `CustomerName` 的列。

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

返回由整数的代码值所给出的字符组成的字符串
`CHAR()` 函数用于获取指定 ASCII 值的对应字符。该函数执行的操作与 `ASCII()` 相反,`ASCII()` 用于返回指定字符的 ASCII 值。

示例:

```sql
SELECT CHAR(65);

+------------+
| CHAR(65) |
+------------+
| A |
+------------+
```

```sql
SELECT CHAR(84);

+------------+
| CHAR(84) |
+------------+
| T |
+------------+
```

`CHAR()` 函数还可用于获取超出标准 ASCII 范围(`0` - `127`)的 ASCII 值的对应字符。

```sql
/*For extended ASCII: */

SELECT CHAR(128);

+------------+
| CHAR(128) |
+------------+
| 0x80 |
+------------+
```

`CHAR()` 函数还可用于获取 Unicode 值的对应字符。

```sql
/* For Unicode: */

--skip-binary-as-hex

SELECT CHAR(50089);

+--------------+
| CHAR(50089) |
+--------------+
| é |
+--------------+
```

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

Expand Down
Loading