Functions for Working with Strings
Functions for searching in strings and for replacing in strings are described separately.
empty
Checks whether the input string is empty. A string is considered non-empty if it contains at least one byte, even if this byte is a space or the null byte.
The function is also available for arrays and UUIDs.
Syntax
empty(x)
Arguments
x
— Input value. String.
Returned value
- Returns
1
for an empty string or0
for a non-empty string. UInt8.
Example
SELECT empty('');
Result:
┌─empty('')─┐
│ 1 │
└───────────┘
notEmpty
Checks whether the input string is non-empty. A string is considered non-empty if it contains at least one byte, even if this byte is a space or the null byte.
The function is also available for arrays and UUIDs.
Syntax
notEmpty(x)
Arguments
x
— Input value. String.
Returned value
- Returns
1
for a non-empty string or0
for an empty string string. UInt8.
Example
SELECT notEmpty('text');
Result:
┌─notEmpty('text')─┐
│ 1 │
└──────────────────┘
length
Returns the length of a string in bytes rather than in characters or Unicode code points. The function also works for arrays.
Alias: OCTET_LENGTH
Syntax
length(s)
Parameters
Returned value
- Length of the string or array
s
in bytes. UInt64.
Example
Query:
SELECT length('Hello, world!');
Result:
┌─length('Hello, world!')─┐
│ 13 │
└─────────────────────────┘
Query:
SELECT length([1, 2, 3, 4]);
Result:
┌─length([1, 2, 3, 4])─┐
│ 4 │
└──────────────────────┘
lengthUTF8
Returns the length of a string in Unicode code points rather than in bytes or characters. It assumes that the string contains valid UTF-8 encoded text. If this assumption is violated, no exception is thrown and the result is undefined.
Aliases:
CHAR_LENGTH
CHARACTER_LENGTH
Syntax
lengthUTF8(s)
Parameters
s
— String containing valid UTF-8 encoded text. String.
Returned value
- Length of the string
s
in Unicode code points. UInt64.
Example
Query:
SELECT lengthUTF8('Здравствуй, мир!');
Result:
┌─lengthUTF8('Здравствуй, мир!')─┐