String Functions
- reference
String functions perform operations on a string input value and returns a string or other value.
If any arguments to any of the following functions are MISSING then the result is also MISSING — that is, no result is returned.
Similarly, if any of the arguments passed to the functions are NULL or are of the wrong type, such as an integer instead of a string, then NULL is returned as the result.
|
CONCAT(string1
, string2
, β¦)
Description
This function takes two or more strings and returns a new string after concatenating the input strings. If there are fewer than two arguments, then it returns an error.
Arguments
- string1, string2, ...
-
[At least 2 are required] The strings, or valid expressions which evaluate to strings, to be concatenated together.
CONCAT2(separator
, arg1
, arg2
, β¦)
Description
This function takes the input strings, or arrays of strings, and concatenates them with the specified separator between each input string. If there are fewer than two arguments, then it returns an error.
Arguments
- separator
-
[Required] The string to separate the input strings. If no separator is required, specify the empty string "".
- arg1, arg2, ...
-
[At least 1 is required] The strings, or arrays of strings, to be concatenated together.
Return Value
A new string, concatenated from the inputs, with the separator between each input. Arrays of strings are flattened and concatenated in the same order. If there is only one string argument, the separator is not used.
If any argument or array element is MISSING, returns MISSING. If any argument or array element is non-string, returns NULL.
CONTAINS(in_str, search_str)
Description
Checks whether or not the specified search string is a substring of the input string — that is, exists within.
This returns true
if the substring exists within the input string, otherwise false
is returned.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to search within.
- search_str
-
A string, or any valid expression which evaluates to a string, that is the string to search for.
INITCAP(in_str)
Description
Converts the string so that the first letter of each word is uppercase and every other letter is lowercase (known as 'Title Case').
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to title case.
LENGTH(in_str)
Equivalent: LEN()
Description
Finds the length of a string, where length is defined as the number of code points within the string.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_LENGTH().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to find the length of.
LOWER(in_str)
Description
Converts all characters in the input string to lower case. This is useful for canonical comparison of string values.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to lower case.
LPAD(in_str, size [, char])
Description
Pads a string with leading characters. The function adds characters to the beginning of the string to pad the string to a specified length.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_LPAD().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to add the leading characters to.
- size
-
An integer, or any valid expression which evaluates to an integer, that specifies the desired length of the result string.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to add to the input string.
If omitted, the default is space
" "
, Unicode U+0020.
Return Value
A string representing the input string with leading characters added.
-
If the specified size is smaller than the length of the input string, the input string is truncated and no padding is added.
-
If the specified size is larger than the length of the input string, but shorter than the length of the input string plus the padding characters, the padding characters are truncated.
-
If the specified size is greater than the length of the input string plus the padding characters, the padding characters are repeated in order until the specified size is reached.
Examples
SELECT LPAD("SQL++ is awesome", 20) AS implicit_padding,
LPAD("SQL++ is awesome", 20, "π!") AS repeated_padding,
LPAD("SQL++ is awesome", 20, "987654321") AS truncate_padding,
LPAD("SQL++ is awesome", 5, "987654321") AS truncate_string;
[
{
"implicit_padding": " SQL++ is awesome",
"repeated_padding": "πSQL++ is awesome", (1)
"truncate_padding": "9876SQL++ is awesome",
"truncate_string": "SQL++"
}
]
1 | The emoji counts as four bytes when calculating the size. |
LTRIM(in_str [, char])
Description
Removes all leading characters from a string. The function removes all consecutive characters from the beginning of the string that match the specified characters and stops when it encounters a character that does not match any of the specified characters.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to remove the leading characters from.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to trim from the input string. Each character in this string is trimmed from the input string — you don’t need to delimit the characters to trim. For example, specifying a character value of
"abc"
trims the characters "a", "b" and "c" from the start of the string.If omitted, the default is whitespace: space
" "
, tab"\t"
, newline"\n"
, formfeed"\f"
, or carriage return"\r"
.
Examples
SELECT LTRIM("...SQL++ is awesome", ".") as dots,
LTRIM(" SQL++ is awesome", " ") as explicit_spaces,
LTRIM("\t SQL++ is awesome") as implicit_spaces,
LTRIM("SQL++ is awesome") as no_dots;
[
{
"dots": "SQL++ is awesome",
"explicit_spaces": "SQL++ is awesome",
"implicit_spaces": "SQL++ is awesome",
"no_dots": "SQL++ is awesome"
}
]
MASK(in_str [, options])
Description
Overlays specified characters in the string with masking characters. This may be useful when returning sensitive information, such as credit card numbers or email addresses.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that represents the string to mask.
- options
-
An object containing the following possible parameters:
- mask
-
A string containing masking characters that will be used to overlay the input string. May optionally also contain hole characters, representing gaps in the mask; and inject characters, that are inserted into the output. (Default:
********
) - hole
-
A string containing the character or characters used to indicate holes in the mask string. (Default: space)
- inject
-
A string containing the character or characters in the mask string that are inserted into the output, rather than overlaying the input. (Default: none)
- length
-
Determines the length of the output string. (Default: missing)
-
If this property is missing, or set to anything other than
"source"
, the length of the output is dynamic. Any characters in the input up to the anchor point (see below) are included in the output. The mask then starts at the anchor point, and continues for the length of the specified mask string. Any characters in the input beyond the end of the mask are deleted. This method may therefore obscure the number of characters in the input. -
If the value is
"source"
, the length of the output is the same as the length of the input. Any characters in the input up to the anchor point are included in the output. The mask then starts at the anchor point. If the mask is longer than the remaining length of the input, the mask is truncated to fit. If the mask string is shorter than or the same length as the remaining length of the input, the mask continues for the length of the specified mask string. Any characters in the input beyond the end of the mask are included in the output.
-
- anchor
-
Determines where in the input string the mask should start. Possible values are
"start"
,"end"
, a regular expression string, a positive integer, or a negative integer. (Default:"start"
)-
"start"
— the mask begins at the start of the input and is applied towards the end. -
"end"
— the mask begins at the end of the input and is applied from the end towards the start. -
Regular expression — the mask begins at the first point in the input which matches the regular expression, and is applied towards the end. If you need to match the strings
"start"
or"end"
, use patterns such as"[s]tart"
or"[e]nd"
. -
Positive integer — the mask begins the specified number of characters after the start of the input, and is applied towards the end.
-
Negative integer — the mask begins the specified number of characters before the end of the input, and is applied towards the start.
If an anchor places the mask outside the boundaries of the input string, the input string is returned unchanged.
-
Examples
Default mask, custom mask, custom mask demonstrating holes.
SELECT MASK('SomeTextToMask') AS mask,
MASK('SomeTextToMask', {"mask": "++++"}) AS mask_custom,
MASK('SomeTextToMask', {"mask": "++++ ++++"}) AS mask_hole;
[
{
"mask": "********",
"mask_custom": "++++",
"mask_hole": "++++Text++++"
}
]
Mask with character injection.
SELECT MASK('1234abcd5678efgh', {"mask": "****-****-****-####",
"hole": "#",
"inject": "-"}) AS mask_inject;
[
{
"mask_inject": "****-****-****-efgh"
}
]
Mask anchored to the end of the source, with the output length determined by the source.
SELECT MASK('1234abcd5678efgh', {"mask": "****", "anchor": "end", "length": "source"})
AS end_anchor;
[
{
"end_anchor": "1234abcd5678****"
}
]
Mask anchored at the pattern d5
.
SELECT MASK('1234abcd5678efgh', {"mask": "****", "anchor": "d5"}) AS regex_anchor;
[
{
"regex_anchor": "1234abc****"
}
]
Mask anchored 2 characters from the end of the source, with length determined by the input string.
SELECT MASK('1234abcd5678efgh', {"mask": "****", "anchor": -2, "length": "source"})
AS negative_anchor;
[
{
"negative_anchor": "1234abcd56****gh"
}
]
Mask anchored at the 14th character, with length determined by the input string.
SELECT MASK('1234abcd5678efgh', {"mask": "****", "anchor": 14, "length": "source"})
AS positive_anchor;
[
{
"positive_anchor": "1234abcd5678ef**"
}
]
MB_LENGTH(in_str)
(Introduced in Couchbase Server 7.6)
Description
Finds the length of a string, where length is defined as the number of characters within the string.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see LENGTH().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to find the length of.
Examples
SELECT MB_LENGTH("SQL++ is awesome") AS ascii,
MB_LENGTH("CafΓ©") AS diacritic,
MB_LENGTH("π") AS emoji,
MB_LENGTH("") AS zero;
[
{
"ascii": 16,
"diacritic": 4, (1)
"emoji": 1, (2)
"zero": 0
}
]
1 | The letter with diacritic counts as a single character. |
2 | The emoji counts as a single character. |
MB_LPAD(in_str, size [, char])
(Introduced in Couchbase Server 7.6)
Description
Pads a string with leading characters. The function adds characters to the beginning of the string to pad the string to a specified length.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see LPAD().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to add the leading characters to.
- size
-
An integer, or any valid expression which evaluates to an integer, that specifies the desired length of the result string.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to add to the input string.
If omitted, the default is space
" "
, Unicode U+0020.
Return Value
A string representing the input string with leading characters added.
-
If the specified size is smaller than the length of the input string, the input string is truncated and no padding is added.
-
If the specified size is larger than the length of the input string, but shorter than the length of the input string plus the padding characters, the padding characters are truncated.
-
If the specified size is greater than the length of the input string plus the padding characters, the padding characters are repeated in order until the specified size is reached.
Examples
SELECT MB_LPAD("SQL++ is awesome", 20) AS implicit_padding,
MB_LPAD("SQL++ is awesome", 20, "π!") AS repeated_padding,
MB_LPAD("SQL++ is awesome", 20, "987654321") AS truncate_padding,
MB_LPAD("SQL++ is awesome", 5, "987654321") AS truncate_string;
[
{
"implicit_padding": " SQL++ is awesome",
"repeated_padding": "π!π!SQL++ is awesome", (1)
"truncate_padding": "9876SQL++ is awesome",
"truncate_string": "SQL++"
}
]
1 | The emoji counts as a single character when calculating the size. |
MB_RPAD(in_str, size [, char])
(Introduced in Couchbase Server 7.6)
Description
Pads a string with trailing characters. The function adds characters to the end of the string to pad the string to a specified length.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see RPAD().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to add the trailing characters to.
- size
-
An integer, or any valid expression which evaluates to an integer, that specifies the desired length of the result string.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to add to the input string.
If omitted, the default is space
" "
, Unicode U+0020.
Return Value
A string representing the input string with trailing characters added.
-
If the specified size is smaller than the length of the input string, the input string is truncated and no padding is added.
-
If the specified size is larger than the length of the input string, but shorter than the length of the input string plus the padding characters, the padding characters are truncated.
-
If the specified size is greater than the length of the input string plus the padding characters, the padding characters are repeated in order until the specified size is reached.
Examples
SELECT MB_RPAD("SQL++ is awesome", 20) AS implicit_padding,
MB_RPAD("SQL++ is awesome", 20, "π!") AS repeated_padding,
MB_RPAD("SQL++ is awesome", 20, "123456789") AS truncate_padding,
MB_RPAD("SQL++ is awesome", 5, "123456789") AS truncate_string;
[
{
"implicit_padding": "SQL++ is awesome ",
"repeated_padding": "SQL++ is awesomeπ!π!", (1)
"truncate_padding": "SQL++ is awesome1234",
"truncate_string": "SQL++"
}
]
1 | The emoji counts as a single character when calculating the size. |
MB_SUBSTR(in_str, start_pos [, length])
(Introduced in Couchbase Server 7.6)
Description
Returns the substring (of given length) counting forward from the provided position. The position is zero-based — that is, the first position is 0. If position is negative, it is counted from the end of the string; -1 is the last position in the string.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see SUBSTR().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to extract the substring from.
- start_pos
-
An integer, or any valid expression which evaluates to an integer, that is the start position of the substring.
- length
-
[Optional; default is to capture to the end of the string]
An integer, or any valid expression which evaluates to an integer, that is the length of the substring to extract.
Examples
SELECT MB_SUBSTR("π SQL++ is awesome", 11) as rest_of_string,
MB_SUBSTR("π SQL++ is awesome", 11, 1) as single_letter,
MB_SUBSTR("π SQL++ is awesome", 0, 10) as ten_from_start;
[
{
"rest_of_string": "awesome",
"single_letter": "a",
"ten_from_start": "π SQL++ is"
}
]
The emoji counts as a single character for the starting position and the substring length.
MB_SUBSTR1(in_str, start_pos [, length])
(Introduced in Couchbase Server 7.6)
Description
Returns the substring (of given length) counting forward from the provided position. The position is one-based — that is, the first position is 1. If position is negative, it is counted from the end of the string; 0 is the last position in the string.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see SUBSTR1().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to extract the substring from.
- start_pos
-
An integer, or any valid expression which evaluates to an integer, that is the start position of the substring.
- length
-
[Optional; default is to capture to the end of the string]
An integer, or any valid expression which evaluates to an integer, that is the length of the substring to extract.
Examples
SELECT MB_SUBSTR1("π SQL++ is awesome", 12) as rest_of_string,
MB_SUBSTR1("π SQL++ is awesome", 12, 1) as single_letter,
MB_SUBSTR1("π SQL++ is awesome", 0, 10) as ten_from_start;
[
{
"rest_of_string": "awesome",
"single_letter": "a",
"ten_from_start": "π SQL++ is"
}
]
The emoji counts as a single character for the starting position and the substring length.
MB_POS(in_str, search_str)
Alias for MB_POSITION().
MB_POS1(in_str, search_str)
Alias for MB_POSITION1().
MB_POSITION(in_str, search_str)
(Introduced in Couchbase Server 7.6)
Description
Finds the first position of the search string within the string. This position is zero-based — that is, the first position is 0. If the search string does not exist within the input string then the function returns -1.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see POSITION().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to search within.
- search_str
-
A string, or any valid expression which evaluates to a string, that is the string to search for.
Examples
SELECT MB_POSITION("π SQL++ is awesome", "awesome") as awesome,
MB_POSITION("π SQL++ is awesome", "N1QL") as n1ql,
MB_POSITION("π SQL++ is awesome", "SQL") as sql;
[
{
"awesome": 11,
"n1ql": -1,
"sql": 2
}
]
The emoji counts as a single character when calculating the position.
MB_POSITION1(in_str, search_str)
(Introduced in Couchbase Server 7.6)
Description
Finds the first position of the search string within the string. This position is one-based — that is, the first position is 1. If the search string does not exist within the input string then the function returns 0.
This function works with multi-byte characters, not single bytes. For a variant of this function that works with single bytes, see POSITION1().
Because this function works with multi-byte characters, it may be slower than its single byte variant.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to search within.
- search_str
-
A string, or any valid expression which evaluates to a string, that is the string to search for.
Examples
SELECT MB_POSITION1("π SQL++ is awesome", "awesome") as awesome,
MB_POSITION1("π SQL++ is awesome", "N1QL") as n1ql,
MB_POSITION1("π SQL++ is awesome", "SQL") as sql;
[
{
"awesome": 12,
"n1ql": 0,
"sql": 3
}
]
The emoji counts as a single character when calculating the position.
POS(in_str, search_str)
Alias for POSITION().
POS1(in_str, search_str)
Alias for POSITION1().
POSITION(in_str, search_str)
Description
Finds the first position of the search string within the string. This position is zero-based — that is, the first position is 0. If the search string does not exist within the input string then the function returns -1.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_POSITION().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to search within.
- search_str
-
A string, or any valid expression which evaluates to a string, that is the string to search for.
POSITION1(in_str, search_str)
Description
Finds the first position of the search string within the string. This position is one-based — that is, the first position is 1. If the search string does not exist within the input string then the function returns 0.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_POSITION1().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to search within.
- search_str
-
A string, or any valid expression which evaluates to a string, that is the string to search for.
REPEAT(in_str, n)
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to repeat.
- n
-
An integer, or any valid expression which evaluates to an integer, that is the number of times to repeat the string.
Limitations
It is possible to generate very large strings using this function. In some cases the query engine may be unable to process all of these and cause excessive resource consumption. It is therefore recommended that you first validate the inputs to this function to ensure that the generated result is a reasonable size.
REPLACE(in_str, search_str, replace [, n ])
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to search for replacements in.
- search_str
-
A string, or any valid expression which evaluates to a string, that is the string to replace.
- replace
-
A string, or any valid expression which evaluates to a string, that is the string to replace the search string with.
- n
-
[Optional; default is all instances of the search string are replaced]
An integer, or any valid expression which evaluates to an integer, which represents the number of instances of the search string to replace. If a negative value is specified then all instances of the search string are replaced.
REVERSE(in_str)
Description
Reverses the order of the characters in a given string. That is, the first character becomes the last character and the last character becomes the first character, and so on. Among other things, you can use this function to test whether or not a string is a palindrome.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to reverse.
RPAD(in_str, size [, char])
Description
Pads a string with trailing characters. The function adds characters to the end of the string to pad the string to a specified length.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_RPAD().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to add the trailing characters to.
- size
-
An integer, or any valid expression which evaluates to an integer, that specifies the desired length of the result string.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to add to the input string.
If omitted, the default is space
" "
, Unicode U+0020.
Return Value
A string representing the input string with trailing characters added.
-
If the specified size is smaller than the length of the input string, the input string is truncated and no padding is added.
-
If the specified size is larger than the length of the input string, but shorter than the length of the input string plus the padding characters, the padding characters are truncated.
-
If the specified size is greater than the length of the input string plus the padding characters, the padding characters are repeated in order until the specified size is reached.
Examples
SELECT RPAD("SQL++ is awesome", 20) AS implicit_padding,
RPAD("SQL++ is awesome", 20, "π!") AS repeated_padding,
RPAD("SQL++ is awesome", 20, "123456789") AS truncate_padding,
RPAD("SQL++ is awesome", 5, "123456789") AS truncate_string;
[
{
"implicit_padding": "SQL++ is awesome ",
"repeated_padding": "SQL++ is awesomeπ", (1)
"truncate_padding": "SQL++ is awesome1234",
"truncate_string": "SQL++"
}
]
1 | The emoji counts as four bytes when calculating the size. |
RTRIM(in_str [, char])
Description
Removes all trailing characters from a string. The function removes all consecutive characters from the end of the string that match the specified characters and stops when it encounters a character that does not match any of the specified characters.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to remove trailing characters from.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to trim from the input string. Each character in this string is trimmed from the input string — you don’t need to delimit the characters to trim. For example, specifying a character value of
"abc"
trims the characters"a"
,"b"
and"c"
from the start of the string.If omitted, the default is whitespace: space
" "
, tab"\t"
, newline"\n"
, formfeed"\f"
, or carriage return"\r"
.
Examples
SELECT RTRIM("SQL++ is awesome...", ".") as dots,
RTRIM("SQL++ is awesome ", " ") as explicit_spaces,
RTRIM("SQL++ is awesome \t") as implicit_spaces,
RTRIM("SQL++ is awesome") as no_dots;
[
{
"dots": "SQL++ is awesome",
"explicit_spaces": "SQL++ is awesome",
"implicit_spaces": "SQL++ is awesome",
"no_dots": "SQL++ is awesome"
}
]
SPLIT(in_str [, in_substr])
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to split.
- in_substr
-
A string, or any valid expression which evaluates to a string, that is the substring to split the input string on.
Examples
SELECT SPLIT("SQL++ is awesome", " ") as explicit_spaces,
SPLIT("SQL++ is awesome") as implicit_spaces,
SPLIT("SQL++ is awesome", "is") as split_is;
[
{
"explicit_spaces": [
"SQL++",
"is",
"awesome"
],
"implicit_spaces": [
"SQL++",
"is",
"awesome"
],
"split_is": [
"SQL++ ",
" awesome"
]
}
]
SUBSTR(in_str, start_pos [, length])
Description
Returns the substring (of given length) counting forward from the provided position. The position is zero-based — that is, the first position is 0. If position is negative, it is counted from the end of the string; -1 is the last position in the string.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_SUBSTR().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to extract the substring from.
- start_pos
-
An integer, or any valid expression which evaluates to an integer, that is the start position of the substring.
- length
-
[Optional; default is to capture to the end of the string]
An integer, or any valid expression which evaluates to an integer, that is the length of the substring to extract.
Examples
SELECT SUBSTR("π SQL++ is awesome", 11) as rest_of_string,
SUBSTR("π SQL++ is awesome", 11, 1) as single_letter,
SUBSTR("π SQL++ is awesome", 0, 10) as ten_from_start;
[
{
"rest_of_string": "is awesome",
"single_letter": "i",
"ten_from_start": "π SQL++"
}
]
The emoji counts as four bytes for the starting position and the substring length.
SUBSTR1(in_str, start_pos [, length])
Description
Returns the substring (of given length) counting forward from the provided position. The position is one-based — that is, the first position is 1. If position is negative, it is counted from the end of the string; 0 is the last position in the string.
This function works with single bytes, not multi-byte characters. For a variant of this function that works with multi-byte characters, see MB_SUBSTR1().
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to extract the substring from.
- start_pos
-
An integer, or any valid expression which evaluates to an integer, that is the start position of the substring.
- length
-
[Optional; default is to capture to the end of the string]
An integer, or any valid expression which evaluates to an integer, that is the length of the substring to extract.
Examples
SELECT SUBSTR1("π SQL++ is awesome", 12) as rest_of_string,
SUBSTR1("π SQL++ is awesome", 12, 1) as single_letter,
SUBSTR1("π SQL++ is awesome", 0, 10) as ten_from_start;
[
{
"rest_of_string": "is awesome",
"single_letter": "i",
"ten_from_start": "π SQL++"
}
]
The emoji counts as four bytes for the starting position and the substring length.
SUFFIXES(in_str)
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to generate the suffixes of.
Examples
SELECT SUFFIXES("SQL++ is awesome") as sqlpp;
[
{
"sqlpp": [
"SQL++ is awesome",
"QL++ is awesome",
"L++ is awesome",
"++ is awesome",
"+ is awesome",
" is awesome",
"is awesome",
"s awesome",
" awesome",
"awesome",
"wesome",
"esome",
"some",
"ome",
"me",
"e"
]
}
]
The following example uses the SUFFIXES()
function to index and query the airport names when a partial airport name is given.
For this example, set the query context to the inventory
scope in the travel sample dataset.
For more information, see Query Context.
CREATE INDEX autocomplete_airport_name
ON airport ( DISTINCT ARRAY array_element FOR array_element
IN SUFFIXES(LOWER(airportname)) END );
SELECT airportname
FROM airport
WHERE ANY array_element
IN SUFFIXES(LOWER(airportname)) SATISFIES array_element LIKE 'washing%' END;
[
{
"airportname": "Washington Dulles Intl"
},
{
"airportname": "Baltimore Washington Intl"
},
{
"airportname": "Ronald Reagan Washington Natl"
},
{
"airportname": "Washington Union Station"
}
]
This blog provides more information about this example.
TITLE(in_str)
Alias for INITCAP().
TOKENS(in_str, opt)
Description
This function tokenizes (i.e. breaks up into meaningful segments) the given input string based on specified delimiters, and other options. It recursively enumerates all tokens in a JSON value and returns an array of values (JSON atomic values) as the result.
Arguments
- in_str
-
A valid JSON object, this can be anything: constant literal, simple JSON value, JSON key name or the whole document itself.
The following table lists the rules for each JSON type:
JSON Type Return Value MISSING
[]
NULL
[NULL]
false
[false]
true
[true]
number
[number]
string
SPLIT(string)
array
FLATTEN(TOKENS(element) for each element in array
(Concatenation of element tokens)
object
For each name-value pair, name+TOKENS(value)
- opt
-
A JSON object indicating the options passed to the
TOKENS()
function. Options can take the following options, and each invocation ofTOKENS()
can choose one or more of the options:- {"name": true}
-
Optional. Valid values are
true
orfalse
. By default, this is set to true andTOKENS()
will include field names. You can choose to not include field names by setting this option tofalse
. - {"case":"lower"}
-
Optional. Valid values are
lower
orupper
. Default is neither, as in it returns the case of the original data. Use this option to specify the case sensitivity. - {"specials": true}
-
Optional. Use this option to preserve strings with specials characters, such as email addresses, URLs, and hyphenated phone numbers. The default value is
false
.The specials
options preserves special characters except at the end of a word.
Examples
By default, for speed, the results are randomly ordered.
To make the difference more clear between the first two example queries, the ARRAY_SORT() function is used.
|
specials
is FALSESELECT ARRAY_SORT(
TOKENS( ['jim@example.com, kim@example.com, http://example.com/, 408-555-1212'],
{'specials': false} ));
[
{
"$1": [
"1212",
"408",
"555",
"abc",
"com",
"http",
"jim",
"kim"
]
}
]
specials
is TRUESELECT ARRAY_SORT(
TOKENS( ['jim@example.com, kim@example.com, http://example.com/, 408-555-1212'],
{'specials': true} ));
[
{
"$1": [
"1212",
"408",
"408-555-1212",
"555",
"abc",
"com",
"http",
"http://example.com",
"jim",
"jim@example.com",
"kim",
"kim@example.com"
]
}
]
For this example, set the query context to the inventory
scope in the travel sample dataset.
For more information, see Query Context.
SELECT ARRAY_SORT( TOKENS(url) ) AS defaulttoken,
ARRAY_SORT( TOKENS(url, {"specials":true, "case":"UPPER"}) ) AS specialtoken
FROM hotel
LIMIT 1;
[
{
"defaulttoken": [
"http",
"org",
"uk",
"www",
"yha"
],
"specialtoken": [
"HTTP",
"HTTP://WWW.YHA.ORG.UK",
"ORG",
"UK",
"WWW",
"YHA"
]
}
]
You can also use {"case":"lower"}
or {"case":"upper"}
to have case sensitive search.
Index creation and querying can use this and other parameters in combination.
These parameters should be passed within the query predicates as well.
The parameters and values must match exactly for SQL++ to pick up and use the index correctly.
case
and use it your applicationFor this example, set the query context to the inventory
scope in the travel sample dataset.
For more information, see Query Context.
CREATE INDEX idx_url_upper_special ON hotel(
DISTINCT ARRAY v FOR v IN
TOKENS(url, {"specials":true, "case":"UPPER"})
END );
SELECT name, address, url
FROM hotel
WHERE ANY v IN TOKENS(url, {"specials":true, "case":"UPPER"})
SATISFIES v = "HTTP://WWW.YHA.ORG.UK"
END;
{
"results": [
{
"address": "Capstone Road, ME7 3JE",
"name": "Medway Youth Hostel",
"url": "http://www.yha.org.uk"
}
]
}
TRIM(in_str [, char])
Description
Removes all leading and trailing characters from a string. The function removes all consecutive characters from the beginning and end of the string that match the specified characters and stops when it encounters a character that does not match any of the specified characters. This function is equivalent to calling LTRIM() and RTRIM() successively.
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to remove trailing and leading characters from.
- char
-
[Optional] A string, or any valid expression which evaluates to a string, that represents the characters to trim from the input string. Each character in this string is trimmed from the input string — you don’t need to delimit the characters to trim. For example, specifying a character value of
"abc"
trims the characters"a"
,"b"
and"c"
from the start of the string.If omitted, the default is whitespace: space
" "
, tab"\t"
, newline"\n"
, formfeed"\f"
, or carriage return"\r"
.
Examples
SELECT TRIM("...SQL++ is awesome...", ".") as dots,
TRIM(" SQL++ is awesome ", " ") as explicit_spaces,
TRIM("\t SQL++ is awesome ") as implicit_spaces,
TRIM("SQL++ is awesome") as no_dots;
[
{
"dots": "SQL++ is awesome",
"explicit_spaces": "SQL++ is awesome",
"implicit_spaces": "SQL++ is awesome",
"no_dots": "SQL++ is awesome"
}
]
UPPER(in_str)
Arguments
- in_str
-
A string, or any valid expression which evaluates to a string, that is the string to convert to upper case.
URLDECODE(encoded_string)
Arguments
- encoded_string
-
A string, or any valid expression which evaluates to a string, that is the string to URL-decode.
URLENCODE(plain_string)
Arguments
- plain_string
-
A string, or any valid expression which evaluates to a string, that is the string to URL-encode.