diff --git a/dbms/tests/queries/0_stateless/01100_split_by_string.reference b/dbms/tests/queries/0_stateless/01100_split_by_string.reference new file mode 100644 index 00000000000..802ad95b1d6 --- /dev/null +++ b/dbms/tests/queries/0_stateless/01100_split_by_string.reference @@ -0,0 +1,7 @@ +['cde','cde'] +['','cde','cde',''] +['','','',''] +['',''] +['a','b','c','d','e'] +['hello','world'] +['gbye','bug'] diff --git a/dbms/tests/queries/0_stateless/01100_split_by_string.sql b/dbms/tests/queries/0_stateless/01100_split_by_string.sql new file mode 100644 index 00000000000..c65c55902ea --- /dev/null +++ b/dbms/tests/queries/0_stateless/01100_split_by_string.sql @@ -0,0 +1,6 @@ +select splitByString('ab', 'cdeabcde'); +select splitByString('ab', 'abcdeabcdeab'); +select splitByString('ab', 'ababab'); +select splitByString('ababab', 'ababab'); +select splitByString('', 'abcde'); +select splitByString(', ', x) from (select arrayJoin(['hello, world', 'gbye, bug']) x); diff --git a/docs/en/query_language/functions/splitting_merging_functions.md b/docs/en/query_language/functions/splitting_merging_functions.md index 514c2165376..5743fd6dc6d 100644 --- a/docs/en/query_language/functions/splitting_merging_functions.md +++ b/docs/en/query_language/functions/splitting_merging_functions.md @@ -5,9 +5,40 @@ Splits a string into substrings separated by 'separator'.'separator' must be a string constant consisting of exactly one character. Returns an array of selected substrings. Empty substrings may be selected if the separator occurs at the beginning or end of the string, or if there are multiple consecutive separators. +**Example:** + +```sql +SELECT splitByChar(',', '1,2,3,abcde') +``` +```text +┌─splitByChar(',', '1,2,3,abcde')─┐ +│ ['1','2','3','abcde'] │ +└─────────────────────────────────┘ +``` + ## splitByString(separator, s) -The same as above, but it uses a string of multiple characters as the separator. The string must be non-empty. +The same as above, but it uses a string of multiple characters as the separator. If the string is empty, it will split the string into an array of single characters. + +**Example:** + +```sql +SELECT splitByString(', ', '1, 2 3, 4,5, abcde') +``` +```text +┌─splitByString(', ', '1, 2 3, 4,5, abcde')─┐ +│ ['1','2 3','4,5','abcde'] │ +└───────────────────────────────────────────┘ +``` + +```sql +SELECT splitByString('', 'abcde') +``` +```text +┌─splitByString('', 'abcde')─┐ +│ ['a','b','c','d','e'] │ +└────────────────────────────┘ +``` ## arrayStringConcat(arr\[, separator\])