You can use string functions to process strings, such as case conversion, space removal, and substring extraction. This topic describes the string function syntax and provides examples on how to use the string functions in AnalyticDB for MySQL.
ASCII: returns the ASCII value of a character or of the leftmost character of a string.
BIN: returns the binary string of an integer.
BIT_LENGTH: returns the length of a string, measured in bits.
CHAR: returns the string that consists of the ASCII values of integers.
CHAR_LENGTH or CHARACTER_LENGTH: returns the length of a string, measured in characters.
CONCAT: concatenates strings.
CONCAT_WS: concatenates strings and separates them with delimiters.
ELT: returns the string specified by the integer N.
ENCRYPT: encrypts a string.
EXPORT_SET: converts an integer into a string based on the bit values of the integer.
FIELD: returns the index position of a specific string in the string list specified by multiple arguments.
FIND_IN_SET: returns the position of a character or string in the comma-separated string list specified by the strlist argument.
FORMAT: formats the number N and returns a string.
FROM_BASE64: decodes a Base64-encoded string and returns the result.
FROM_UTF8: decodes a UTF-8 encoded string and returns the result.
HEX: converts an integer or a string into a hexadecimal string.
INSTR: returns the position of the first occurrence of a substring in a string.
LEFT: returns the N leftmost characters of a string.
LENGTH or OCTET_LENGTH: returns the length of a string.
LIKE: performs simple pattern matching.
LOCATE: returns the position of the first occurrence of a string in another string.
LOWER or LCASE: converts a string to lowercase.
LPAD: returns a string that is left-padded with another string.
LTRIM: removes the leading spaces of a string.
MAKE_SET: returns a set of comma-separated strings.
MID: returns a substring of a specific length that starts from a specific position in a string. The function is equivalent to SUBSTR or SUBSTRING.
OCT: returns the octal string of an integer.
ORD: returns the code of the leftmost character of a string if the character is a multibyte character.
POSITION: returns the position of the first occurrence of a substring in a string.
REPEAT: returns a string that consists of the source string repeated the specified number of times.
REPLACE: replaces specific characters in a string with another string.
REVERSE: reverses the characters in a string.
RIGHT: returns the N rightmost characters of a string.
RLIKE or REGEXP: performs pattern matching of a string against a regular expression. If the string matches the regular expression, 1 is returned. Otherwise, 0 is returned.
RPAD: returns a string that is right-padded with another string.
RTRIM: removes the trailing spaces of a string.
SPACE: returns a string that consists of a specific number of spaces.
SPLIT: splits a string with delimiters and returns an array.
SPLIT_PART: splits a string with delimiters and returns a specific substring in the array.
SPLIT_TO_MAP: splits a string by using
entryDelimiterandkeyValueDelimiterand returns amap.STRCMP: returns 0, 1, or -1 based on the comparison results of two strings.
SUBSTR or SUBSTRING: returns a substring of a specific length that starts from a specific position in a string.
SUBSTRING_INDEX: returns a substring that appears before or after the count-th occurrence of a delimiter in a string.
TO_BASE64: returns a Base64-encoded string.
TO_UTF8: returns a UTF-8 encoded string.
TRIM: removes the leading and trailing spaces of a string.
UPPER or UCASE: converts a string to uppercase.
UNHEX: interprets each pair of hexadecimal digits in the argument as a byte and converts it into a character.
ASCII
ascii(str)Description: This function returns the decimal ASCII value of the
strcharacter or of the leftmost character of thestrstring.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Examples:
Sample statement:
SELECT ascii('2');Sample result:
+------------+ | ascii('2') | +------------+ | 50 | +------------+Sample statement:
SELECT ascii('dx');Sample result:
+-------------+ | ascii('dx') | +-------------+ | 100 | +-------------+
BIN
bin(N)Description: This function returns the binary string of
N.If
Nisnull,NULLis returned.Data type of the input value: BIGINT.
Data type of the return value: VARCHAR.
Example:
SELECT bin(12);Sample result:
+---------+ | bin(12) | +---------+ | 1100 | +---------+
BIT_LENGTH
bit_length(str)Description: This function returns the length of the
strstring, measured in bits.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Examples:
Sample statement:
SELECT bit_length('text');Sample result:
+--------------------+ | bit_length('text') | +--------------------+ | 32 | +--------------------+Sample statement:
SELECT bit_length('China');Sample result:
+---------------------+ | bit_length('China') | +---------------------+ | 40 | +---------------------+
CHAR
char(N1, N2, ...)Description: This function returns the string that consists of the decimal ASCII values of the specified integers.
Data type of the input value: BIGINT.
Data type of the return value: VARBINARY.
Example:
SELECT char(97,110,97,108,121,116,105,99,100,98);Sample result:
+-------------------------------------------+ | char(97,110,97,108,121,116,105,99,100,98) | +-------------------------------------------+ | analyticdb | +-------------------------------------------+
CHAR_LENGTH or CHARACTER_LENGTH
char_length(str)
character_length(str)Description: This function returns the length of the
strstring, measured in characters.The length of a Chinese character is
1.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Examples:
Sample statement:
SELECT char_length('China');Sample result:
+----------------------+ | char_length('China') | +----------------------+ | 5 | +----------------------+Sample statement:
SELECT char_length('abc');Sample result:
+--------------------+ | char_length('abc') | +--------------------+ | 3 | +--------------------+
CONCAT
concat(str 1, …, str n)Description: This function concatenates strings. If an argument is
null,nullis returned.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Examples:
Sample statement:
SELECT concat('aliyun', ', ', 'analyticdb');Sample result:
+--------------------------------------+ | concat('aliyun', ', ', 'analyticdb') | +--------------------------------------+ | aliyun, analyticdb | +--------------------------------------+Sample statement:
SELECT concat('abc',null,'def');Sample result:
+--------------------------+ | concat('abc',null,'def') | +--------------------------+ | NULL | +--------------------------+
CONCAT_WS
concat_ws(separator, str 1, …, str n)Description: This function concatenates strings and separates them with delimiters. The
separatorargument specifies the delimiter for other arguments.nullstrings are skipped.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Examples:
Sample statement:
SELECT concat_ws(',', 'First name', 'Second name', 'Last Name')AS result;Sample result:
+----------------------------------+ | result | +----------------------------------+ | First name,Second name,Last Name | +----------------------------------+Sample statement:
SELECT concat_ws(',','First name',NULL,'Last Name')AS result;Sample result:
+----------------------+ | result | +----------------------+ | First name,Last Name | +----------------------+
ELT
elt(N, str 1, ...,str n);Description: This function returns the
Nth string.If
Nis less than 1 or is greater than the number of string arguments,nullis returned.Data type of the input value: BIGINT for the
Nargument. VARCHAR for thestrarguments.Data type of the return value: VARCHAR.
Example:
SELECT elt(4, 'Aa', 'Bb', 'Cc', 'Dd');Sample result:
+--------------------------------+ | elt(4, 'Aa', 'Bb', 'Cc', 'Dd') | +--------------------------------+ | Dd | +--------------------------------+
ENCRYPT
encrypt(x, y);Description: This function encrypts the
xargument by usingyas the salt value.Data type of the input value: VARBINARY for the
xargument. VARCHAR for theyargument.Data type of the return value: VARBINARY.
Example:
SELECT encrypt('abdABC123','key');Sample result:
+--------------------------------------------------------+ | encrypt('abdABC123','key') | +--------------------------------------------------------+ | 0x6B657A617A6D63496F2E614377 | +--------------------------------------------------------+
EXPORT_SET
export_set(bits, onstr, offstr [, separator[,number_of_bits]]);Description: This function converts an integer specified by the
bitsargument into a binary value. Argument description:onstr: the value that is used to replace 1s in the binary value.offstr: the value that is used to replace 0s in the binary value.separator: the delimiter that is used to separate the returned values.number_of_bits: the number of bits in the binary value that the system checks from right to left. If thenumber_of_bitsvalue is greater than 64 or is -1, 64 bits are returned.
Data type of the input value: BIGINT for the
bitsandnumber_of_bitsarguments. VARCHAR for theonstr,offstr, andseparatorarguments.Data type of the return value: VARCHAR.
Examples:
The following statement converts 5 into a binary value, and returns the two rightmost bits of the binary value by replacing 1s with
aand 0s withband separating the returned values with commas (,).SELECT export_set(5,'a','b',',',2);Sample result:
+-----------------------------+ | export_set(5,'a','b',',',2) | +-----------------------------+ | a,b | +-----------------------------+The following statement converts 6 into a binary value, and returns the 10 rightmost bits of the binary value by using
1for the 1s and0for the 0s in the binary value and separating the returned values with commas (,).SELECT export_set(6,'1','0',',',10);Sample result:
+------------------------------+ | export_set(6,'1','0',',',10) | +------------------------------+ | 0,1,1,0,0,0,0,0,0,0 | +------------------------------+
FIELD
FIELD(str, str 1, str 2,..., str n);Description: This function returns the index position of the
strvalue in the string list specified by multiple arguments.If thestrvalue is not found in the string list,0is returned.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Example:
SELECT FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');Sample result:
sq+-------------------------------------------+ | FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff') | +-------------------------------------------+ | 2 | +-------------------------------------------+
FIND_IN_SET
find_in_set(str, strlist) Description: This function returns the position of the
strvalue in the comma-separated string list specified by thestrlistargument.If the
strvalue is not found instrlistor ifstrlistis an empty list,0is returned.If
strorstrlistisnull,nullis returned.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Example:
SELECT find_in_set('b','a,b,c,d');Sample result:
+----------------------------+ | find_in_set('b','a,b,c,d') | +----------------------------+ | 2 | +----------------------------+
FORMAT
format(X, D)Description: This function formats the number
Xto the#,###,###.##format rounded toDdecimal places and returns the result as a string.If
Dis0, the result does not have the decimal point or fractional part.Data type of the input value: DOUBLE for the
Xargument. BIGINT for theDargument.Data type of the return value: VARCHAR.
Example:
SELECT format(12332.123456, 4)AS result1, format(12332.1,4)AS result2, format(12332.2,0)AS result3;Sample result:
+-------------+-------------+---------+ | result1 | result2 | result3 | +-------------+-------------+---------+ | 12,332.1235 | 12,332.1000 | 12,332 | +-------------+-------------+---------+
FROM_BASE64
from_base64(x)Description: This function decodes the Base64-encoded
xstring and returns the result.Data type of the input value: VARBINARY or VARCHAR.
Data type of the return value: VARBINARY.
NoteYou can use the following methods to convert the VARBINARY-typed result into a VARCHAR value:
For AnalyticDB for MySQL clusters of V3.1.4 or later, use the
CAST AS VARCHARfunction. For more information, see CAST functions.For AnalyticDB for MySQL clusters earlier than V3.1.4, use the
FROM_UTF8function. For more information, see the "FROM_UTF8" section of this topic.
Examples:
The following statement decodes a Base64-encoded string of the VARCHAR type.
SELECT from_base64('Q2hpbmE=');Sample result:
+--------------------------------------------------+ | from_base64('Q2hpbmE=') | +--------------------------------------------------+ | 0x4368696E61 | +--------------------------------------------------+NoteThe preceding returned value is of the VARBINARY type. If you want the system to return a VARCHAR-typed value, execute the following statement:
SELECT cast(from_base64('Q2hpbmE=') AS varchar);Sample result:
+------------------------------------------+ | cast(from_base64('Q2hpbmE=') AS varchar) | +------------------------------------------+ | China | +------------------------------------------+The following statement decodes a Base64-encoded string of the VARBINARY type.
SELECT from_base64(cast(to_base64('China') AS varbinary));Sample result:
+--------------------------------------------------------------------------------------------------------+ | from_base64(cast(to_base64('China') AS varbinary)) | +--------------------------------------------------------------------------------------------------------+ | 0x4368696E61 | +--------------------------------------------------------------------------------------------------------+
FROM_UTF8
from_utf8(x)
from_utf8(x, y)Description:
from_utf8(x): decodes the UTF-8 encodedxvalue and returns the result.from_utf8(x, y): decodes thexvalue that is not UTF-8 encoded and replaces invalid characters in the decoding result with a specific character.Noteyis optional. If you do not specifyy,�is returned for each invalid character in the decoding result.The
yvalue can be a character, such as a number sign (#), or the ASCII value of a character, such as35.
Data type of the input value: VARBINARY for the
xargument. VARCHAR or BIGINT for theyargument.Data type of the return value: VARCHAR.
Examples:
The following statement decodes a UTF-8 encoded value.
SELECT from_utf8(to_utf8('hello'));Sample result:
+-----------------------------+ | from_utf8(to_utf8('hello')) | +-----------------------------+ | hello | +-----------------------------+The following statement decodes a value that is not UTF-8 encoded.
SELECT from_utf8(unhex('58BF'));Sample result:
+--------------------------+ | from_utf8(unhex('58BF')) | +--------------------------+ | X� | +--------------------------+The following statement decodes a value that is not UTF-8 encoded and replaces each invalid character in the decoding result with a number sign (
#).SELECT from_utf8(unhex('58BF'), '#');Sample result:
+-------------------------------+ | from_utf8(unhex('58BF'), '#') | +-------------------------------+ | X# | +-------------------------------+The following statement decodes a value that is not UTF-8 encoded and replaces each invalid character in the decoding result with the character whose ASCII value is 35.
SELECT from_utf8(unhex('58BF'), '35');Sample result:
+-------------------------------+ | from_utf8(unhex('58BF'), '35') | +-------------------------------+ | X# | +-------------------------------+
HEX
hex(x)Description: This function converts the
xvalue into a hexadecimal string.Data type of the input value: BIGINT or VARCHAR.
Data type of the return value: VARCHAR.
Examples:
The following statement converts a BIGINT-typed value.
SELECT hex(16);Sample result:
+---------+ | hex(16) | +---------+ | 10 | +---------+The following statement converts a VARCHAR-typed value.
SELECT hex('16');Sample result:
+-----------+ | hex('16') | +-----------+ | 3136 | +-----------+
INSTR
instr(str, substr)Description: This function returns the position of the first occurrence of the
substrsubstring in thestrstring.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Example:
SELECT instr('foobarbar', 'bar');Sample result:
+---------------------------+ | instr('foobarbar', 'bar') | +---------------------------+ | 4 | +---------------------------+
LEFT
LEFT(str, len)Description: This function returns a number of leftmost characters of the
strstring. The number is specified by thelenargument.If
strorlenisnull,nullis returned.Data type of the input value: VARCHAR for the
strargument. BIGINT for thelenargument.Data type of the return value: VARCHAR.
Example:
SELECT LEFT('foobarbar', 5);Sample result:
+----------------------+ | LEFT('foobarbar', 5) | +----------------------+ | fooba | +----------------------+
LENGTH or OCTET_LENGTH
length(str)
octet_length(str)Description: This function returns the length of the
strstring.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Example:
SELECT length('aliyun');Sample result:
+------------------+ | length('aliyun') | +------------------+ | 6 | +------------------+
LIKE
expression [NOT] LIKE pattern [ESCAPE 'escape_char']Description: This function performs pattern matching of the
expressionstring against thepattern. If the string matches the pattern,1is returned. Otherwise,0is returned.Argument description:patterncan contain the following wildcards:%: matches a string of any length._: matches a single character.
escape_char: escapes the percent signs (%) and underscores (_) in thepatternvalue so that the percent signs (%) and underscores (_) that follow the escape character do not serve as wildcards.
Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Examples:
Sample statement:
SELECT 'David!' LIKE 'David_' AS result1, 'David!' NOT LIKE 'David_' AS result2, 'David!' LIKE '%D%v%' AS result3;Sample result:
+---------+---------+---------+ | result1 | result2 | result3 | +---------+---------+---------+ | 1 | 0 | 1 | +---------+---------+---------+Sample statement:
SELECT 'David_' LIKE 'David|_' ESCAPE '|';Sample result:
+----------------------------------+ | David_' LIKE 'David|_' ESCAPE '| | +----------------------------------+ | 1 | +----------------------------------+
LOCATE
locate(substr, str)
locate(substr, str, pos)Description: This function returns the position of the first occurrence of the
substrsubstring in thestrstring, or returns the position of the first occurrence of thesubstrsubstring in thestrstring, starting from theposposition.If the
substrsubstring is not found in thestrstring,0is returned.If
substrorstrisnull,nullis returned.Data type of the input value: VARCHAR for the
strandsubstrarguments. BIGINT for theposargument.Data type of the return value: BIGINT.
Examples:
Sample statement:
SELECT locate('bar', 'foobarbar');Sample result:
+----------------------------+ | locate('bar', 'foobarbar') | +----------------------------+ | 4 | +----------------------------+Sample statement:
SELECT locate('bar', 'foobarbar', 7);Sample result:
+-------------------------------+ | locate('bar', 'foobarbar', 7) | +-------------------------------+ | 7 | +-------------------------------+
LOWER or LCASE
lower(str)
lcase(str)Description: This function converts letters in the
strstring to lowercase.Data type of the input value: VARCHAR.
Data type of the returned value: VARCHAR.
Example:
SELECT lower('Aliyun');Sample result:
+-----------------+ | lower('Aliyun') | +-----------------+ | aliyun | +-----------------+
LPAD
lpad(str, len, padstr)Description: This function returns the
strstring that is left-padded with thepadstrstring to a length oflencharacters.If the length of the
strstring is greater thanlencharacters, the return value is shortened tolencharacters.Data type of the input value: VARCHAR for the
strandpadstrarguments. BIGINT for thelenargument.Data type of the return value: VARCHAR.
Example:
SELECT lpad('Aliyun',9,'#');Sample result:
+----------------------+ | lpad('Aliyun',9,'#') | +----------------------+ | ###Aliyun | +----------------------+
LTRIM
ltrim(str)Description: This function removes the leading spaces of the
strstring.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Example:
SELECT ltrim(' abc');Sample result:
+----------------+ | ltrim(' abc') | +----------------+ | abc | +----------------+
MAKE_SET
make_set(bits, str 1, str 2,...);Description: This function returns a set value, which is a string that contains substrings separated by delimiters. The set value consists of strings that have the corresponding bits within the bit set.
The
str1string corresponds to bit0. Thestr2string corresponds to bit1. The rest may be deduced by analogy. Thenullvalues in thestr1,str2, and other strings are not appended to the result.Data type of the input value: BIGINT for the
bitsargument. VARCHAR for thestrargument.Data type of the return value: VARCHAR.
Examples:
Sample statement:
SELECT make_set(5,'hello','nice','world');Sample result:
+------------------------------------+ | make_set(5,'hello','nice','world') | +------------------------------------+ | hello,world | +------------------------------------+Sample statement:
SELECT make_set(1 | 4,'hello','nice',NULL,'world')AS result;Sample result:
+--------+ | result | +--------+ | hello | +--------+
MID
mid(str, pos, len)Description: This function returns a substring that contains
lencharacters in length from thestrstring, starting from theposposition. This function is equivalent to SUBSTR or SUBSTRING.Data type of the input value: VARCHAR for the
strargument. BIGINT for theposandlenarguments.Data type of the returned value: VARCHAR.
Examples:
Sample statement:
SELECT mid('Quadratically',5,6);Sample result:
+--------------------------+ | mid('Quadratically',5,6) | +--------------------------+ | ratica | +--------------------------+Sample statement:
SELECT mid('Sakila', -5, 3);Sample result:
+----------------------+ | mid('Sakila', -5, 3) | +----------------------+ | aki | +----------------------+
OCT
oct(N)Description: This function returns the octal string of the integer
N.If
Nisnull,nullis returned.Data type of the input value: BIGINT.
Data type of the return value: VARCHAR.
Example:
SELECT oct(12);Sample result:
+---------+ | oct(12) | +---------+ | 14 | +---------+
ORD
ord(x)Description: This function returns the code of the leftmost character of the
xstring if the character is a multibyte character.Data type of the input value: VARBINARY or VARCHAR.
Data type of the return value: LONG.
Examples:
The following statement returns the code of the leftmost character of a VARCHAR-typed value.
SELECT ord('China');Sample result:
+--------------+ | ord('China') | +--------------+ | 67 | +--------------+The following statement returns the code of the leftmost character of a VARBINARY-typed value.
SELECT ord(cast('China' AS varbinary));Sample result:
+---------------------------------+ | ord(cast('China' AS varbinary)) | +---------------------------------+ | 67 | +---------------------------------+
POSITION
position(substr IN str);Description: This function returns the position of the first occurrence of the
substrsubstring in thestrstring, starting from position1. If the substring is not found in the str string,0is returned.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Example:
SELECT position('bar' in 'foobarbar');Sample result:
+--------------------------------+ | position('bar' in 'foobarbar') | +--------------------------------+ | 4 | +--------------------------------+
REPEAT
repeat(str, count);Description: This function returns a string that consists of the
strstring repeated the number of times specified bycount.If the
countvalue is less than 1, an empty string is returned.If
strorcountisnull,nullis returned.Data type of the input value: VARCHAR for the
strargument. BIGINT for thecountargument.Data type of the return value: VARCHAR.
Examples:
Sample statement:
SELECT repeat('a', 3);Sample result:
+----------------+ | repeat('a', 3) | +----------------+ | aaa | +----------------+Sample statement:
SELECT repeat('abc', null);Sample result:
+---------------------+ | repeat('abc', null) | +---------------------+ | NULL | +---------------------+Sample statement:
SELECT repeat(null, 3);Sample result:
+-----------------+ | repeat(null, 3) | +-----------------+ | NULL | +-----------------+
REPLACE
replace(str, from_str, to_str);Description: This function replaces all
from_strstrings in thestrstring with theto_strstring.Data type of the input value: VARCHAR.
Data type of the returned value: VARCHAR.
Example:
SELECT replace('WWW.aliyun.com', 'W', 'w');Sample result:
+-------------------------------------+ | replace('WWW.aliyun.com', 'W', 'w') | +-------------------------------------+ | www.aliyun.com | +-------------------------------------+
REVERSE
reverse(str);Description: This function returns the
strstring with the order of the characters reversed.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Example:
SELECT reverse('123456');Sample result:
+-------------------+ | reverse('123456') | +-------------------+ | 654321 | +-------------------+
RIGHT
RIGHT(str, len);Description: This function returns the rightmost
lencharacters of thestrstring.If
strorlenisnull,nullis returned.Data type of the input value: VARCHAR for the
strargument. BIGINT for thelenargument.Data type of the return value: VARCHAR.
Example:
SELECT RIGHT('abc',3);Sample result:
+----------------+ | RIGHT('abc',3) | +----------------+ | abc | +----------------+
RLIKE or REGEXP
expression RLIKE pattern;
expression REGEXP pattern;Description: This function performs pattern matching of the
expressionstring against the regular expression specified bypattern. If the string matches the regular expression,1is returned. Otherwise,0is returned.If
expressionorpatternisnull,nullis returned.Data type of the input value: VARCHAR.
Data type of the return value: BOOLEAN.
Examples:
Sample statement:
SELECT 'Michael!' REGEXP '.*';Sample result:
+----------------------+ | Michael!' REGEXP '.* | +----------------------+ | 1 | +----------------------+Sample statement:
SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';Sample result:
+-------------------------------------+ | new*\n*line' REGEXP 'new\\*.\\*line | +-------------------------------------+ | 0 | +-------------------------------------+Sample statement:
SELECT 'c' REGEXP '^[a-d]';Sample result:
+-------------------+ | c' REGEXP '^[a-d] | +-------------------+ | 1 | +-------------------+
RPAD
rpad(str, len, padstr)Description: This function returns the
strstring that is right-padded with thepadstrstring to a length oflencharacters.If the length of the
strstring is greater thanlencharacters, the return value is shortened tolencharacters.Data type of the input value: VARCHAR for the
strandpadstrarguments. BIGINT for thelenargument.Data type of the return value: VARCHAR.
Example:
SELECT rpad('Aliyun',9,'#');Sample result:
+----------------------+ | rpad('Aliyun',9,'#') | +----------------------+ | Aliyun### | +----------------------+
RTRIM
rtrim(str)Description: This function removes the trailing spaces of the
strstring.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Example:
SELECT rtrim('barbar ');Sample result:
+--------------------+ | rtrim('barbar ') | +--------------------+ | barbar | +--------------------+
SPACE
space(N);Description: This function returns a string that consists of a specific number of spaces.
NoteWe recommend that you use this function together with the
contact()function to facilitate display of results.Data type of the input value: BIGINT.
Data type of the return value: VARCHAR.
Example:
SELECT concat("#", space(6), "#");Sample result:
+----------------------------+ | concat("#", space(6), "#") | +----------------------------+ | # # | +----------------------------+
SPLIT
split(string, delimiter)Description: This function splits the
stringwith thedelimiterand returns an array.Data type of the input value: VARCHAR.
Data type of the return value: ARRAY<varchar>.
Example:
SELECT split('1#2#3', '#'), split('#1#2#3#', '#'), split('123', '#');Sample result:
+---------------------+-----------------------+-------------------+ | split('1#2#3', '#') | split('#1#2#3#', '#') | split('123', '#') | +---------------------+-----------------------+-------------------+ | ["1","2","3"] | ["","1","2","3",""] | ["123"] | +---------------------+-----------------------+-------------------+
SPLIT_PART
split_part(string, delimiter, index)Description: This function splits the
stringwith thedelimiterand returns the substring whose array subscript is theindexvalue. Theindexvalue starts from 1. If the value of index is greater than the number of substrings, NULL is returned.Data type of the input value: VARCHAR for the
stringanddelimiterarguments. BIGINT for theindexargument.Data type of the return value: VARCHAR.
Example:
SELECT split_part('A#B#C', '#', 2), split_part('A#B#C', '#', 4);Sample result:
+-----------------------------+-----------------------------+ | split_part('A#B#C', '#', 2) | split_part('A#B#C', '#', 4) | +-----------------------------+-----------------------------+ | B | NULL | +-----------------------------+-----------------------------+
SPLIT_TO_MAP
split_to_map(string, entryDelimiter, keyValueDelimiter)Description: This function splits a string by using
entryDelimiterandkeyValueDelimiterand returns amap. The string is split byentryDelimiterintokey-valuepairs, and thekey-valuepairs are split bykeyValueDelimiterintokeysandvalues.Data type of the input value: VARCHAR.
Data type of the return value: MAP<varchar, varchar>.
Example:
SELECT split_to_map('k1:v1,k2:v2', ',', ':'),split_to_map('', ',', ':');Sample result:
+---------------------------------------+----------------------------+ | split_to_map('k1:v1,k2:v2', ',', ':') | split_to_map('', ',', ':') | +---------------------------------------+----------------------------+ | {"k1":"v1","k2":"v2"} | {} | +---------------------------------------+----------------------------+
STRCMP
strcmp(str 1, str 2);Description: This function compares the
str 1andstr 2strings. If they are identical,0is returned. If thestr 1value is less than thestr 2value,-1is returned. Otherwise,1is returned.Data type of the input value: VARCHAR.
Data type of the return value: BIGINT.
Example:
SELECT strcmp('text', 'text2');Sample result:
+-------------------------+ | strcmp('text', 'text2') | +-------------------------+ | -1 | +-------------------------+
SUBSTR or SUBSTRING
substr(str, pos)
substr(str FROM pos)
substr(str, pos, len)
substr(str FROM pos FOR len)
substring(str, pos)
substring(str FROM pos)
substring(str, pos, len)
substring(str FROM pos FOR len)Description:
SUBSTRING(varchar str, bigint pos)orSUBSTRING(varchar str FROM pos): returns the substring that starts from theposposition to the end of the string. If theposvalue is less than 0, the substring starts from the position that is pos characters away from the end of the string.SUBSTRING(varchar str, bigint pos, bigint len)orSUBSTRING(varchar str FROM pos FOR len): returns a substring that containslencharacters in length from the string, starting from theposposition. If theposvalue is less than 0, the substring starts from the position that is pos characters away from the end of the string.
Data type of the input value: VARCHAR for the
strargument. BIGINT for theposandlenarguments.Data type of the return value: VARCHAR.
Examples:
Sample statement:
SELECT substr('helloworld', 6);Sample result:
+-------------------------+ | substr('helloworld', 6) | +-------------------------+ | world | +-------------------------+Sample statement:
SELECT substr('helloworld' FROM 6);Sample result:
+-----------------------------+ | substr('helloworld' FROM 6) | +-----------------------------+ | world | +-----------------------------+Sample statement:
SELECT substr('helloworld', 6, 3);Sample result:
+----------------------------+ | substr('helloworld', 6, 3) | +----------------------------+ | wor | +----------------------------+Sample statement:
SELECT substr('helloworld' from 6 for 3);Sample result:
+-----------------------------------+ | substr('helloworld' FROM 6 FOR 3) | +-----------------------------------+ | wor | +-----------------------------------+
SUBSTRING_INDEX
substring_index(str, delim, count)Description: This function returns a substring that appears before or after the count-th occurrence of the
delimdelimiter in thestrstring.If the
countvalue is greater than 0, this function returns all characters to the left of the count-th occurrence of thedelimdelimiter.If the
countvalue is less than 0, this function returns all characters to the right of the count-th occurrence of thedelimdelimiter.The
SUBSTRING_INDEXfunction performs a case-sensitive match when it searches for thedelimdelimiter.Data type of the input value: VARCHAR for the
stranddelimarguments. BIGINT for thecountargument.Data type of the returned value: VARCHAR.
Example:
SELECT substring_index('www.aliyun.com', '.', 2);Sample result:
+-------------------------------------------+ | substring_index('www.aliyun.com', '.', 2) | +-------------------------------------------+ | www.aliyun | +-------------------------------------------+
TO_BASE64
to_base64(x)Description: This function returns the
xstring encoded in the Base64 format.Data type of the input value: VARBINARY or VARCHAR.
Data type of the return value: VARCHAR.
Examples:
The following statement returns the Base64-encoded string of a VARCHAR-typed value.
SELECT to_base64('China');Sample result:
+--------------------+ | to_base64('China') | +--------------------+ | Q2hpbmE= | +--------------------+The following statement returns the Base64-encoded string of a VARBINARY-typed value.
SELECT to_base64(cast('China' AS varbinary));Sample result:
+---------------------------------------+ | to_base64(cast('China' AS varbinary)) | +---------------------------------------+ | Q2hpbmE= | +---------------------------------------+
TO_UTF8
to_utf8(x)Description: This function returns the
xstring encoded in the UTF-8 format.Data type of the input value: VARCHAR.
Data type of the returned value: VARCHAR.
Example:
SELECT from_utf8(to_utf8('China'));Sample result:
+-----------------------------+ | from_utf8(to_utf8('China')) | +-----------------------------+ | China | +-----------------------------+
TRIM
trim([remstr FROM] str)
trim([{BOTH | LEADING | TRAILING} [remstr] FROM] str)Description: This function removes the leading and trailing spaces or the characters contained in the optional
remstrstring from thestrstring.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Examples:
Sample statement:
SELECT trim(' bar ');Sample result:
+------------------+ | trim(' bar ') | +------------------+ | bar | +------------------+Sample statement:
SELECT trim(BOTH 'x' FROM 'xxxbarxxx');Sample result:
+---------------------------------+ | trim(BOTH 'x' FROM 'xxxbarxxx') | +---------------------------------+ | bar | +---------------------------------+Sample statement:
SELECT trim(LEADING 'x' FROM 'xxxbarxxx');Sample result:
+------------------------------------+ | trim(LEADING 'x' FROM 'xxxbarxxx') | +------------------------------------+ | barxxx | +------------------------------------+Sample statement:
SELECT trim(TRAILING 'x' from 'xxxbarxxx');Sample result:
+-------------------------------------+ | trim(TRAILING 'x' from 'xxxbarxxx') | +-------------------------------------+ | xxxbar | +-------------------------------------+
UPPER or UCASE
upper(str)
ucase(str)Description: This function converts letters in the
strstring to uppercase.Data type of the input value: VARCHAR.
Data type of the return value: VARCHAR.
Example:
SELECT upper('Aliyun');Sample result:
+-----------------+ | upper('Aliyun') | +-----------------+ | ALIYUN |
UNHEX
unhex(x);Description: This function interprets each pair of hexadecimal digits in the
xargument as a byte and converts each byte into a character.Data type of the input value: VARBINARY or VARCHAR.
Data type of the return value: VARBINARY.
NoteYou can use the following methods to convert the VARBINARY-typed result into a VARCHAR value:
For AnalyticDB for MySQL clusters of V3.1.4 or later, use the
CAST AS VARCHARfunction. For more information, see CAST functions.For AnalyticDB for MySQL clusters earlier than V3.1.4, use the
FROM_UTF8function. For more information, see the "FROM_UTF8" section of this topic.
If the input value contains non-hexadecimal digits, NULL is returned.
Examples:
The following statement decodes a Base64-encoded string of the VARCHAR type.
SELECT unhex(hex('China'));Sample result:
+------------------------------------------+ | unhex(hex('China')) | +------------------------------------------+ | China | +------------------------------------------+NoteThe preceding returned value is of the VARBINARY type. If you want the system to return a VARCHAR-typed value, execute the following statement:
SELECT cast(unhex(hex('China')) AS varchar);Sample result:
+--------------------------------------+ | cast(unhex(hex('China')) AS varchar) | +--------------------------------------+ | China | +--------------------------------------+The following statement decodes a Base64-encoded string of the VARBINARY type.
SELECT unhex(cast(hex('China') AS varbinary));Sample result:
+------------------------------------------+ | unhex(cast(hex('China') AS varbinary)) | +------------------------------------------+ | China | +------------------------------------------+