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

Add examples for ORD() #16956

Merged
merged 8 commits into from
Apr 23, 2024
Merged
Changes from 7 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
55 changes: 52 additions & 3 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ SELECT CustomerName, CHAR_LENGTH(CustomerName) AS LenghtOfName FROM Customers;

The `CHARACTER_LENGTH()` function is the same as the `CHAR_LENGTH()` function. Both functions can be used synonymously because they generate the same output.

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

The `CONCAT()` function concatenates one or more arguments into a single string.

Expand Down Expand Up @@ -1188,11 +1188,60 @@ Return a string containing octal representation of a number.

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

Synonym for `LENGTH()`.
Synonym for [`LENGTH()`](#length).

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

Return character code for leftmost character of the argument.
Return the character code for the leftmost character of the given argument.

This function is similar to [`CHAR()`](#char) but works the other way around.

Examples:

Taking `a` and `A` as an example, `ORD()` returns `97` for `a` and `65` for `A`.

```sql
SELECT ORD('a'), ORD('A');
```

```
+----------+----------+
| ORD('a') | ORD('A') |
+----------+----------+
| 97 | 65 |
+----------+----------+
1 row in set (0.00 sec)
```

If you take the character code obtained from `ORD()` as input, you can get the original characters back using the `CHAR()` function. Note that the output format might vary depending on whether the `binary-as-hex` option is enabled in your MySQL client.

```sql
SELECT CHAR(97), CHAR(65);
```

```
+----------+----------+
| CHAR(97) | CHAR(65) |
+----------+----------+
| a | A |
+----------+----------+
1 row in set (0.01 sec)
```

The following example shows how `ORD()` handles multibyte characters. Here, both `101` and `0x65` have the UTF-8 encoded value for the `e` character, but in different formats. And both `50091` and `0xC3AB` have the same value, but for the `ë` character.
qiancai marked this conversation as resolved.
Show resolved Hide resolved

```sql
SELECT ORD('e'), ORD('ë'), HEX('e'), HEX('ë');
```

```
+----------+-----------+----------+-----------+
| ORD('e') | ORD('ë') | HEX('e') | HEX('ë') |
+----------+-----------+----------+-----------+
| 101 | 50091 | 65 | C3AB |
+----------+-----------+----------+-----------+
1 row in set (0.00 sec)
```

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

Expand Down
Loading