From dae0484191b1a4b84ed91112030cdda4d0cce477 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 2 Jan 2024 20:16:48 +0800 Subject: [PATCH 01/12] update functions-and-operators/string-functions.md: add instructions for using CONCAT() and CONCAT_WS() (cherry picked from commit 39915731e2e107f144bcaf482ae1a1682154be43) --- functions-and-operators/string-functions.md | 96 +++++++++++++++++++-- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 36dc29748a879..c53f64e85c589 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -40,13 +40,92 @@ 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) - -Return concatenated string. - -### [`CONCAT_WS()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws) - -Return concatenate with separator. +### [`CONCAT(str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat) + +The `CONCAT()` function is used to concatenate one or more arguments. + +```sql +SELECT CONCAT('TiDB', NULL, 'Server'); ++--------------------------------+ +| CONCAT('TiDB', NULL, 'Server') | ++--------------------------------+ +| NULL | ++--------------------------------+ +``` + +`CONCAT()` returns `NULL` if any argument is `NULL`. + +```sql +SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, true); ++---------------------------------------------+ +| CONCAT('TiDB', ' ', 'Server', '-', 1, true) | ++---------------------------------------------+ +| TiDB Server-11 | ++---------------------------------------------+ +``` + +Otherwise, concatenation can be performed by placing the strings next to each other + +```sql +SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, true); ++---------------------------------------------+ +| CONCAT('TiDB', ' ', 'Server', '-', 1, true) | ++---------------------------------------------+ +| TiDB Server-11 | ++---------------------------------------------+ +``` + +### [`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 used to concatenate one or more arguments with the separator and is a form of CONCAT() with th separator. +The first argument is the separator, while the rest of the argument is used for concatenation. + +```sql +SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD'); ++---------------------------------------------+ +| CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD') | ++---------------------------------------------+ +| TiDB Server,TiKV,PD | ++---------------------------------------------+ +``` + +- `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 | ++--------------------------------------------+ + +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 | ++-------------------------------------------+ + +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) @@ -269,8 +348,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 From 9b3e57d6a0491d4c87c8512e757f9ba269e45127 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 9 Jan 2024 20:12:30 +0800 Subject: [PATCH 02/12] Update functions-and-operators/string-functions.md --- functions-and-operators/string-functions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 89743398c1405..5b1dc6e11ae1e 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -137,7 +137,7 @@ SELECT CONCAT('TiDB', NULL, 'Server'); `CONCAT()` returns `NULL` if any argument is `NULL`. ```sql -SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, true); +SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TURE); +---------------------------------------------+ | CONCAT('TiDB', ' ', 'Server', '-', 1, true) | +---------------------------------------------+ @@ -148,7 +148,7 @@ SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, true); Otherwise, concatenation can be performed by placing the strings next to each other ```sql -SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, true); +SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TURE); +---------------------------------------------+ | CONCAT('TiDB', ' ', 'Server', '-', 1, true) | +---------------------------------------------+ @@ -158,7 +158,7 @@ SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, true); ### [`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 used to concatenate one or more arguments with the separator and is a form of CONCAT() with th separator. +The `CONCAT_WS()` function is used to concatenate one or more arguments with a separator and is a form of CONCAT() but with a separator. The first argument is the separator, while the rest of the argument is used for concatenation. ```sql From 2e7a88a8c5b63b476117a0b128b52caba3b523cb Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 9 Jan 2024 20:13:53 +0800 Subject: [PATCH 03/12] fix --- functions-and-operators/string-functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 5b1dc6e11ae1e..f094ecc211c3e 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -137,7 +137,7 @@ SELECT CONCAT('TiDB', NULL, 'Server'); `CONCAT()` returns `NULL` if any argument is `NULL`. ```sql -SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TURE); +SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); +---------------------------------------------+ | CONCAT('TiDB', ' ', 'Server', '-', 1, true) | +---------------------------------------------+ @@ -148,7 +148,7 @@ SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TURE); Otherwise, concatenation can be performed by placing the strings next to each other ```sql -SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TURE); +SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); +---------------------------------------------+ | CONCAT('TiDB', ' ', 'Server', '-', 1, true) | +---------------------------------------------+ From b5ecf4212647b7d4b81b7cb4b56d658d34579682 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 9 Jan 2024 20:20:19 +0800 Subject: [PATCH 04/12] true -> TRUE --- functions-and-operators/string-functions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index f094ecc211c3e..a140a72ecc280 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -139,7 +139,7 @@ SELECT CONCAT('TiDB', NULL, 'Server'); ```sql SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); +---------------------------------------------+ -| CONCAT('TiDB', ' ', 'Server', '-', 1, true) | +| CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE) | +---------------------------------------------+ | TiDB Server-11 | +---------------------------------------------+ @@ -150,7 +150,7 @@ Otherwise, concatenation can be performed by placing the strings next to each ot ```sql SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); +---------------------------------------------+ -| CONCAT('TiDB', ' ', 'Server', '-', 1, true) | +| CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE) | +---------------------------------------------+ | TiDB Server-11 | +---------------------------------------------+ From c2adaa06f61aaea6bac2ee8c2469c2c724765a65 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 9 Jan 2024 20:37:31 +0800 Subject: [PATCH 05/12] placing the string FIX --- functions-and-operators/string-functions.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index a140a72ecc280..c8598391be74d 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -148,12 +148,12 @@ SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); Otherwise, concatenation can be performed by placing the strings next to each other ```sql -SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); -+---------------------------------------------+ -| CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE) | -+---------------------------------------------+ -| TiDB Server-11 | -+---------------------------------------------+ +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) From 1bf6fed1f247ad6b2da830b809e6370725e9bbf3 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 9 Jan 2024 20:55:24 +0800 Subject: [PATCH 06/12] fix format and examples --- functions-and-operators/string-functions.md | 36 +++++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index c8598391be74d..1330eb1df4da8 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -124,9 +124,20 @@ Synonym for `CHAR_LENGTH()`. ### [`CONCAT(str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat) The `CONCAT()` function is used to concatenate one or more arguments. +```sql +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'); + +--------------------------------+ | CONCAT('TiDB', NULL, 'Server') | +--------------------------------+ @@ -134,21 +145,11 @@ SELECT CONCAT('TiDB', NULL, 'Server'); +--------------------------------+ ``` -`CONCAT()` returns `NULL` if any argument is `NULL`. - -```sql -SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); -+---------------------------------------------+ -| CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE) | -+---------------------------------------------+ -| TiDB Server-11 | -+---------------------------------------------+ -``` - Otherwise, concatenation can be performed by placing the strings next to each other ```sql SELECT 'Ti' 'DB' ' ' 'Server'; + +-------------+ | Ti | +-------------+ @@ -158,11 +159,12 @@ SELECT 'Ti' 'DB' ' ' '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 used to concatenate one or more arguments with a separator and is a form of CONCAT() but with a separator. +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. ```sql SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD'); + +---------------------------------------------+ | CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD') | +---------------------------------------------+ @@ -175,13 +177,16 @@ SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD'); ```sql SELECT CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD'); + +--------------------------------------------+ | CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD') | +--------------------------------------------+ | TiDB ServerTiKVPD | +--------------------------------------------+ - +``` +```sql SELECT CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD'); + +----------------------------------------------+ | CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD') | +----------------------------------------------+ @@ -194,13 +199,16 @@ SELECT CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD'); ```sql SELECT CONCAT_WS(',', 'TiDB Server', NULL, 'PD'); + +-------------------------------------------+ | CONCAT_WS(',', 'TiDB Server', NULL, 'PD') | +-------------------------------------------+ | TiDB Server,PD | +-------------------------------------------+ - +``` +```sql SELECT CONCAT_WS(',', 'TiDB Server', '', 'PD'); + +-----------------------------------------+ | CONCAT_WS(',', 'TiDB Server', '', 'PD') | +-----------------------------------------+ From 0214377951ad00de84f0eec79b0f5139241290fe Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Tue, 9 Jan 2024 20:59:06 +0800 Subject: [PATCH 07/12] fix --- functions-and-operators/string-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 1330eb1df4da8..2c8676300bd4b 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -123,7 +123,7 @@ Synonym for `CHAR_LENGTH()`. ### [`CONCAT(str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat) -The `CONCAT()` function is used to concatenate one or more arguments. +The `CONCAT()` function concatenates one or more arguments. ```sql SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); From a9c82d5bc8383637c5623fa6133860761b45b04a Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Wed, 10 Jan 2024 00:24:44 +0800 Subject: [PATCH 08/12] fix MANUAL LINE BREAKS --- functions-and-operators/string-functions.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 2c8676300bd4b..b4411c0efda9b 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -124,6 +124,7 @@ Synonym for `CHAR_LENGTH()`. ### [`CONCAT(str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat) The `CONCAT()` function concatenates one or more arguments. + ```sql SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); @@ -135,6 +136,7 @@ SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); ``` `CONCAT()` returns `NULL` if any argument is `NULL`. + ```sql SELECT CONCAT('TiDB', NULL, 'Server'); @@ -159,8 +161,7 @@ SELECT 'Ti' 'DB' ' ' '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. +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. ```sql SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD'); @@ -184,6 +185,7 @@ SELECT CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD'); | TiDB ServerTiKVPD | +--------------------------------------------+ ``` + ```sql SELECT CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD'); @@ -206,6 +208,7 @@ SELECT CONCAT_WS(',', 'TiDB Server', NULL, 'PD'); | TiDB Server,PD | +-------------------------------------------+ ``` + ```sql SELECT CONCAT_WS(',', 'TiDB Server', '', 'PD'); From 906024977cb86f9f396ece79867ffdd67a93f6c4 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Wed, 10 Jan 2024 01:04:15 +0800 Subject: [PATCH 09/12] manual line breaks FIX --- functions-and-operators/string-functions.md | 1 + 1 file changed, 1 insertion(+) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index b4411c0efda9b..767126a951391 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -441,6 +441,7 @@ The value options of `match_type` between TiDB and MySQL are: - 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. + - TiDB does not support `"u"`, which means Unix-only line endings in MySQL. ### Data type compatibility From 8eabaa65d94e9053d7b0fd23119963bd1e9b42d7 Mon Sep 17 00:00:00 2001 From: Weaxs <459312872@qq.com> Date: Wed, 10 Jan 2024 01:09:47 +0800 Subject: [PATCH 10/12] rollback line443 --- functions-and-operators/string-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 767126a951391..8132fef3eda89 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -440,7 +440,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. From 1491dc6d3494370787764bd21776d6ca6487319f Mon Sep 17 00:00:00 2001 From: qiancai Date: Fri, 26 Jan 2024 17:55:34 +0800 Subject: [PATCH 11/12] refine descriptions and formats --- functions-and-operators/string-functions.md | 175 +++++++++++++++----- 1 file changed, 129 insertions(+), 46 deletions(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index 8132fef3eda89..d066603beb3e7 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -121,103 +121,186 @@ Return number of characters in argument. Synonym for `CHAR_LENGTH()`. -### [`CONCAT(str1,str2,...)`](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) -The `CONCAT()` function concatenates one or more arguments. +The `CONCAT()` function concatenates one or more arguments into a single string. + +Syntax: + +```sql +CONCAT(str1,str2,...) +``` + +`str1, str2, ...` is a list of arguments to be concatenated. Each argument can be a string or a number. + +Example: ```sql SELECT CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE); +``` + +Output: +```sql +---------------------------------------------+ | CONCAT('TiDB', ' ', 'Server', '-', 1, TRUE) | +---------------------------------------------+ -| TiDB Server-11 | +| TiDB Server-11 | +---------------------------------------------+ ``` -`CONCAT()` returns `NULL` if any argument is `NULL`. +If any of the arguments is `NULL`, `CONCAT()` returns `NULL`. + +Example: ```sql SELECT CONCAT('TiDB', NULL, 'Server'); +``` + +Output: +```sql +--------------------------------+ | CONCAT('TiDB', NULL, 'Server') | +--------------------------------+ -| NULL | +| NULL | +--------------------------------+ ``` -Otherwise, concatenation can be performed by placing the strings next to each other +In addition to the `CONCAT()` function, you can concatenate strings by placing them adjacent to each other as in the following example, but this method does not support numeric types. ```sql SELECT 'Ti' 'DB' ' ' 'Server'; +``` + +Output: +```sql +-------------+ -| Ti | +| Ti | +-------------+ | TiDB Server | +-------------+ ``` -### [`CONCAT_WS(separator,str1,str2,...)`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws) +### [`CONCAT_WS()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws) + +The `CONCAT_WS()` function is a form of [`CONCAT()`](#concat) with a separator, which returns a string concatenated by the specified separator. + +Syntax: + +```sql +CONCAT_WS(separator,str1,str2,...) +``` + +- `separator`: the first argument is the separator, which concatenates the remaining arguments that are not `NULL`. +- `str1, str2, ...`: a list of arguments to be concatenated. Each argument can be a string or a number. -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. +Example: ```sql SELECT CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD'); +``` + +Output: +```sql +---------------------------------------------+ | CONCAT_WS(',', 'TiDB Server', 'TiKV', 'PD') | +---------------------------------------------+ -| TiDB Server,TiKV,PD | +| TiDB Server,TiKV,PD | +---------------------------------------------+ ``` -- `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`. +- If the separator is an empty string, `CONCAT_WS()` is equivalent to `CONCAT()` and returns the concatenated string of the remaining arguments. -```sql -SELECT CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD'); + Example: -+--------------------------------------------+ -| CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD') | -+--------------------------------------------+ -| TiDB ServerTiKVPD | -+--------------------------------------------+ -``` + ```sql + SELECT CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD'); + ``` -```sql -SELECT CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD'); + Output: -+----------------------------------------------+ -| CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD') | -+----------------------------------------------+ -| NULL | -+----------------------------------------------+ -``` + ```sql + +--------------------------------------------+ + | CONCAT_WS('', 'TiDB Server', 'TiKV', 'PD') | + +--------------------------------------------+ + | TiDB ServerTiKVPD | + +--------------------------------------------+ + ``` -- `CONCAT_WS()` skips `NULL` arguments after the separator argument. -- `CONCAT_WS()` does not skips `EMPTY` string after the separator argument. +- If the separator is `NULL`, `CONCAT_WS()` returns `NULL`. -```sql -SELECT CONCAT_WS(',', 'TiDB Server', NULL, 'PD'); + Example: -+-------------------------------------------+ -| CONCAT_WS(',', 'TiDB Server', NULL, 'PD') | -+-------------------------------------------+ -| TiDB Server,PD | -+-------------------------------------------+ -``` + ```sql + SELECT CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD'); + ``` -```sql -SELECT CONCAT_WS(',', 'TiDB Server', '', 'PD'); + Output: -+-----------------------------------------+ -| CONCAT_WS(',', 'TiDB Server', '', 'PD') | -+-----------------------------------------+ -| TiDB Server,,PD | -+-----------------------------------------+ -``` + ```sql + +----------------------------------------------+ + | CONCAT_WS(NULL, 'TiDB Server', 'TiKV', 'PD') | + +----------------------------------------------+ + | NULL | + +----------------------------------------------+ + ``` + +- If only one of the arguments to be concatenated is not `NULL`, `CONCAT_WS()` returns that argument. + + Example: + + ```sql + SELECT CONCAT_WS(',', 'TiDB Server', NULL); + ``` + + Output: + + ```sql + +-------------------------------------+ + | CONCAT_WS(',', 'TiDB Server', NULL) | + +-------------------------------------+ + | TiDB Server | + +-------------------------------------+ + ``` + +- If there are `NULL` arguments to be concatenated, `CONCAT_WS()` skips these `NULL` arguments. + + Example: + + ```sql + SELECT CONCAT_WS(',', 'TiDB Server', NULL, 'PD'); + ``` + + Output: + + ```sql + +-------------------------------------------+ + | CONCAT_WS(',', 'TiDB Server', NULL, 'PD') | + +-------------------------------------------+ + | TiDB Server,PD | + +-------------------------------------------+ + ``` + +- If there are empty strings to be concatenated, `CONCAT_WS()` does not skip empty strings. + + Example: + + ```sql + SELECT CONCAT_WS(',', 'TiDB Server', '', 'PD'); + ``` + + Output: + + ```sql + +-----------------------------------------+ + | CONCAT_WS(',', 'TiDB Server', '', 'PD') | + +-----------------------------------------+ + | TiDB Server,,PD | + +-----------------------------------------+ + ``` ### [`ELT()`](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_elt) From 832a5c46408c0a2a605bcc1aa25e325356057645 Mon Sep 17 00:00:00 2001 From: Grace Cai Date: Tue, 30 Jan 2024 14:22:47 +0800 Subject: [PATCH 12/12] Update functions-and-operators/string-functions.md Co-authored-by: xixirangrang --- functions-and-operators/string-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions-and-operators/string-functions.md b/functions-and-operators/string-functions.md index d066603beb3e7..7b563a3fa85bb 100644 --- a/functions-and-operators/string-functions.md +++ b/functions-and-operators/string-functions.md @@ -167,7 +167,7 @@ Output: +--------------------------------+ ``` -In addition to the `CONCAT()` function, you can concatenate strings by placing them adjacent to each other as in the following example, but this method does not support numeric types. +In addition to the `CONCAT()` function, you can concatenate strings by placing them adjacent to each other as in the following example. Note that this method does not support numeric types. ```sql SELECT 'Ti' 'DB' ' ' 'Server';