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 CONCAT() and CONCAT_WS() #16048

Merged
merged 13 commits into from
Jan 30, 2024
98 changes: 92 additions & 6 deletions functions-and-operators/string-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,100 @@ Return number of characters in argument.

Synonym for `CHAR_LENGTH()`.

### [`CONCAT()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat)
### [`CONCAT(str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat)

Return concatenated string.
The `CONCAT()` function is used to concatenate one or more arguments.
Weaxs marked this conversation as resolved.
Show resolved Hide resolved
```sql
Weaxs marked this conversation as resolved.
Show resolved Hide resolved
SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE);

+---------------------------------------------+
| CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE) |
+---------------------------------------------+
| TiDB Server-11 |
+---------------------------------------------+
```

`CONCAT()` returns `NULL` if any argument is `NULL`.
```sql
SELECT CONCAT('TiDB', NULL, 'Server');
Weaxs marked this conversation as resolved.
Show resolved Hide resolved

### [`CONCAT_WS()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws)
+--------------------------------+
| CONCAT('TiDB', NULL, 'Server') |
+--------------------------------+
| NULL |
+--------------------------------+
```

Otherwise, concatenation can be performed by placing the strings next to each other

```sql
SELECT 'Ti' 'DB' ' ' 'Server';

+-------------+
| Ti |
+-------------+
| TiDB Server |
+-------------+
```

### [`CONCAT_WS(separator,str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws)

The `CONCAT_WS()`function is similar to the `CONCAT()` function and concatenates one or more arguments, but does this with a separator that is specified by the user.
The first argument is the separator, while the rest of the argument is used for concatenation.
Weaxs marked this conversation as resolved.
Show resolved Hide resolved

```sql
SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD');

+---------------------------------------------+
| CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD') |
+---------------------------------------------+
| TiDB Server,TiKV,PD |
+---------------------------------------------+
```

Return concatenate with separator.
- `CONCAT_WS()` returns the concatenation of the rest of the argument if the separator is `EMPTY` string.
- `CONCAT_WS()` returns `NULL` if the separator is `NULL`.

```sql
SELECT CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD');

+--------------------------------------------+
| CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD') |
+--------------------------------------------+
| TiDB ServerTiKVPD |
+--------------------------------------------+
```
```sql
Weaxs marked this conversation as resolved.
Show resolved Hide resolved
SELECT CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD');

+----------------------------------------------+
| CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD') |
+----------------------------------------------+
| NULL |
+----------------------------------------------+
```

- `CONCAT_WS()` skips `NULL` arguments after the separator argument.
- `CONCAT_WS()` does not skips `EMPTY` string after the separator argument.

```sql
SELECT CONCAT_WS(',', 'TiDB Server', NULL, 'PD');

+-------------------------------------------+
| CONCAT_WS(',', 'TiDB Server', NULL, 'PD') |
+-------------------------------------------+
| TiDB Server,PD |
+-------------------------------------------+
```
```sql
Weaxs marked this conversation as resolved.
Show resolved Hide resolved
SELECT CONCAT_WS(',', 'TiDB Server', '', 'PD');

+-----------------------------------------+
| CONCAT_WS(',', 'TiDB Server', '', 'PD') |
+-----------------------------------------+
| TiDB Server,,PD |
+-----------------------------------------+
```

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

Expand Down Expand Up @@ -350,8 +437,7 @@ The value options of `match_type` between TiDB and MySQL are:
- Value options in TiDB are `"c"`, `"i"`, `"m"`, and `"s"`, and value options in MySQL are `"c"`, `"i"`, `"m"`, `"n"`, and `"u"`.
- The `"s"` in TiDB corresponds to `"n"` in MySQL. When `"s"` is set in TiDB, the `.` character also matches line terminators (`\n`).

For example, the `SELECT REGEXP_LIKE(a, b, "n") FROM t1` in MySQL is the same as the `SELECT REGEXP_LIKE(a, b, "s") FROM t1` in TiDB.

For example, the `SELECT REGEXP_LIKE(a, b, "n") FROM t1` in MySQL is the same as the `SELECT REGEXP_LIKE(a, b, "s") FROM t1` in TiDB.
- TiDB does not support `"u"`, which means Unix-only line endings in MySQL.

### Data type compatibility
Expand Down
Loading