Use string functions to process strings. For example, you can convert character cases, remove spaces, or extract substrings. This topic describes the syntax of string functions in AnalyticDB for MySQL and provides examples.
ASCII: Returns the ASCII value of a character or the leftmost character in a string.
BIN: Returns the binary string representation of an integer.
BIT_LENGTH: Returns the length of a string in bits.
CHAR: Returns a string composed of characters corresponding to the decimal ASCII codes of integers.
CHAR_LENGTH or CHARACTER_LENGTH: Returns the length of a string, in characters.
CONCAT: Concatenates multiple strings.
CONCAT_WS: Concatenates multiple strings using a specified separator.
ELT: Returns the Nth string from a given list of strings.
ENCRYPT: Encrypts a string.
EXPORT_SET: Returns a combined string based on the bit values of an integer.
FIELD: Returns the index of a specified string within a list of strings.
FIND_IN_SET: Returns the position of a string within a comma-separated list.
FORMAT: Formats the number N and returns a string.
FROM_BASE64: Decodes a Base64-encoded string and returns the decoded result.
FROM_UTF8: Decodes a UTF-8 encoded string and returns the decoded result.
HEX: Converts an integer or a string to its hexadecimal string representation.
INSTR: Returns the position of the first occurrence of a substring within 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 substring within another string.
LOWER or LCASE: Converts a string to lowercase.
LPAD: Left-pads a string.
LTRIM: Removes leading spaces from a string.
MAKE_SET: Returns a set of comma-separated strings.
MD5_MUR: Converts a string to a numeric value.
MID: Returns a substring of a specified length from a specified position within a string. This function is the same as SUBSTR or SUBSTRING.
OCT: Returns the octal string representation of a specified integer.
ORD: Returns the code of the leftmost character if it is a multibyte character.
POSITION: Returns the position of the first occurrence of a substring within a string.
REPEAT: Returns a string repeated a specified number of times.
REPLACE: Replaces a part of a string with a specified string.
REVERSE: Reverses a string.
RIGHT: Returns a specified number of the rightmost characters of a string.
RLIKE or REGEXP: Matches a string against a regular expression. It returns 1 if the string matches the pattern, and 0 otherwise.
RPAD: Right-pads a string.
RTRIM: Removes trailing spaces from a string.
SPACE: Returns a string consisting of a specified number of spaces.
SPLIT: Splits a string by a delimiter and returns an array.
SPLIT_PART: Splits a string by a delimiter and returns the substring at a specified index.
SPLIT_TO_MAP: Splits a string using an
entryDelimiterand akeyValueDelimiter, then returns amap.STRCMP: Compares two strings and returns 0 if they are identical, -1 if the first string is smaller than the second based on the current sorting order, or 1 otherwise.
SUBSTR or SUBSTRING: Returns a substring of a specified length from a specified position.
SUBSTRING_INDEX: Returns a substring from a string, either before or after a specified number of occurrences of a delimiter.
TO_BASE64: Returns the Base64-encoded form of a string.
TO_UTF8: Returns the UTF-8 encoded form of a string.
TRIM: Removes all leading and trailing spaces from a string.
UPPER or UCASE: Converts a string to uppercase.
UNHEX: Converts hexadecimal numbers to characters.
ASCII
ascii(str)Description: Returns the decimal ASCII value of the leftmost character of either the character
stror the stringstr.Input parameter data type: VARCHAR.
Return value data type: BIGINT.
Examples:
Statement:
SELECT ascii('2');The following result is returned:
+------------+ | ascii('2') | +------------+ | 50 | +------------+Statement:
SELECT ascii('dx');The following result is returned:
+-------------+ | ascii('dx') | +-------------+ | 100 | +-------------+
BIN
bin(N)Description: Returns the binary string representation of
N.If
Nisnull,NULLis returned.Input parameter data type: BIGINT.
Return value data type: VARCHAR.
Example:
SELECT bin(12);The following result is returned:
+---------+ | bin(12) | +---------+ | 1100 | +---------+
BIT_LENGTH
bit_length(str)Description: Returns the length of the string
strin bits.Input parameter data type: VARCHAR.
Return value data type: BIGINT.
Examples:
Statement:
SELECT bit_length('text');The following result is returned:
+--------------------+ | bit_length('text') | +--------------------+ | 32 | +--------------------+Statement:
SELECT bit_length('China');The following result is returned:
+---------------------+ | bit_length('China') | +---------------------+ | 40 | +---------------------+
CHAR
char(N1, N2, ...)Description: Returns a string composed of characters corresponding to the decimal ASCII codes of the integers
N1,N2, and so on.Input parameter data type: BIGINT.
Return value data type: VARBINARY.
Example:
SELECT char(97,110,97,108,121,116,105,99,100,98);The following result is returned:
+-------------------------------------------+ | char(97,110,97,108,121,116,105,99,100,98) | +-------------------------------------------+ | analyticdb | +-------------------------------------------+
CHAR_LENGTH or CHARACTER_LENGTH
char_length(str)
character_length(str)Description: Returns the length of the string
str, in characters.The length of a Chinese character is
1.Input parameter data type: VARCHAR.
Return value data type: BIGINT.
Examples:
Statement:
SELECT char_length('China');The following result is returned:
+----------------------+ | char_length('China') | +----------------------+ | 5 | +----------------------+Statement:
SELECT char_length('abc');The following result is returned:
+--------------------+ | char_length('abc') | +--------------------+ | 3 | +--------------------+
CONCAT
concat(str 1, …, str n)Description: Concatenates multiple strings. If any parameter is
null, the return value isnull.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Examples:
Statement:
SELECT concat('aliyun', ', ', 'analyticdb');The following result is returned:
+--------------------------------------+ | concat('aliyun', ', ', 'analyticdb') | +--------------------------------------+ | aliyun, analyticdb | +--------------------------------------+Statement:
SELECT concat('abc',null,'def');The following result is returned:
+--------------------------+ | concat('abc',null,'def') | +--------------------------+ | NULL | +--------------------------+
CONCAT_WS
concat_ws(separator, str 1, …, str n)Description: Concatenates multiple strings using a specified separator. The first parameter
separatoris the delimiter for the other parameters. Anynullstrings are skipped during concatenation.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Examples:
Statement:
SELECT concat_ws(',', 'First name', 'Second name', 'Last Name')AS result;The following result is returned:
+----------------------------------+ | result | +----------------------------------+ | First name,Second name,Last Name | +----------------------------------+Statement:
SELECT concat_ws(',','First name',NULL,'Last Name')AS result;The following result is returned:
+----------------------+ | result | +----------------------+ | First name,Last Name | +----------------------+
ELT
elt(N, str 1, ...,str n);Returns the
Nth string.If
N<1or N is greater than the number of string parameters,nullis returned.Input parameter data type:
Nis of the BIGINT type, andstris of the VARCHAR type.Return value data type: VARCHAR.
Example:
SELECT elt(4, 'Aa', 'Bb', 'Cc', 'Dd');The following result is returned:
+--------------------------------+ | elt(4, 'Aa', 'Bb', 'Cc', 'Dd') | +--------------------------------+ | Dd | +--------------------------------+
ENCRYPT
encrypt(x, y);Description: Encrypts the parameter
x.yis the salt value.Input parameter data type:
xis of the VARBINARY type, andyis of the VARCHAR type.Return value data type: VARBINARY
Example:
SELECT encrypt('abdABC123','key');The following result is returned:
+--------------------------------------------------------+ | encrypt('abdABC123','key') | +--------------------------------------------------------+ | 0x6B657A617A6D63496F2E614377 | +--------------------------------------------------------+
EXPORT_SET
export_set(bits, onstr, offstr [, separator[,number_of_bits]]);Description: Converts
bitsto a binary value. The parameters are described as follows:The system checks the binary value from right to left. If a bit is 1, it is replaced with the
onstrvalue. If a bit is 0, it is replaced with theoffstrvalue.The return values are separated by the
separator.number_of_bitsspecifies the number of bits to check. The default value is 64. If you specify a value greater than 64 fornumber_of_bits, the value is trimmed to 64. If you specify -1 fornumber_of_bits, the default value 64 is used.
Input parameter data type:
bitsandnumber_of_bitsare of the BIGINT type.onstr,offstr, andseparatorare of the VARCHAR type.Return value data type: VARCHAR.
Examples:
Convert 5 to a binary value, and take the first two bits from right to left. Replace 1 with
aand 0 withb. Separateaandbwith a comma (,). The statement is as follows:SELECT export_set(5,'a','b',',',2);The following result is returned:
+-----------------------------+ | export_set(5,'a','b',',',2) | +-----------------------------+ | a,b | +-----------------------------+Convert 6 to a binary value, and take the first 10 bits from right to left. Replace 1 with
1and 0 with0. Separate1and0with a comma (,). The statement is as follows:SELECT export_set(6,'1','0',',',10);The following result is returned:
+------------------------------+ | 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: Returns the index of
strwithin the list ofstr 1,str 2, ...,str n. Ifstris not found,0is returned.Input parameter data type: VARCHAR.
Return value data type: BIGINT.
Example:
SELECT FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');The following result is returned:
sq+-------------------------------------------+ | FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff') | +-------------------------------------------+ | 2 | +-------------------------------------------+
FIND_IN_SET
find_in_set(str, strlist) Description: Returns the position of
strwithin the liststrlist.If
stris not instrlistor ifstrlistis an empty string,0is returned.If either the
strorstrlistparameter isnull,nullis returned.Input parameter data type:
strandstrlistare of the VARCHAR type.Return value data type: BIGINT.
Example:
SELECT find_in_set('b','a,b,c,d');The following result is returned:
+----------------------------+ | find_in_set('b','a,b,c,d') | +----------------------------+ | 2 | +----------------------------+
FORMAT
format(X, D)Description: Formats the number
Xinto a#,###,###.##style, rounds it toDdecimal places, and returns the result as a string.If
Dis0, the result has no decimal point or fractional part.Input parameter data type:
Xis of the DOUBLE type, andDis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT format(12332.123456, 4)AS result1, format(12332.1,4)AS result2, format(12332.2,0)AS result3;The following result is returned:
+-------------+-------------+---------+ | result1 | result2 | result3 | +-------------+-------------+---------+ | 12,332.1235 | 12,332.1000 | 12,332 | +-------------+-------------+---------+
FROM_BASE64
from_base64(x)Description: Decodes the Base64-encoded parameter
xand returns the decoded result.Input parameter data type: VARBINARY or VARCHAR.
Return value data type: VARBINARY.
NoteThe decoded return value is of the VARBINARY type. To convert the result to the VARCHAR type, use one of the following methods:
For AnalyticDB for MySQL clusters of version 3.1.4 or later, you can use the
CAST AS VARCHARfunction to convert data types. For more information, see the CAST function.For AnalyticDB for MySQL clusters earlier than V3.1.4, use the
FROM_UTF8function to convert data types. For more information, see FROM_UTF8.
Examples:
The input parameter is of the VARCHAR type. The statement is as follows:
SELECT from_base64('Q2hpbmE=');The following result is returned:
+--------------------------------------------------+ | from_base64('Q2hpbmE=') | +--------------------------------------------------+ | 0x4368696E61 | +--------------------------------------------------+NoteThe return value of the preceding statement is of the VARBINARY type. To obtain the original VARCHAR value, run the following statement:
SELECT cast(from_base64('Q2hpbmE=') AS varchar);The following result is returned:
+------------------------------------------+ | cast(from_base64('Q2hpbmE=') AS varchar) | +------------------------------------------+ | China | +------------------------------------------+The input parameter is of the VARBINARY type. The statement is as follows:
SELECT from_base64(cast(to_base64('China') AS varbinary));The following result is returned:
+--------------------------------------------------------------------------------------------------------+ | 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 encodedxand returns the decoded result.from_utf8(x, y): Decodes the parameterxas a UTF-8 string, replacing any invalid characters with a specified character.NoteThe
yparameter is optional. If you do not specifyy,is returned by default.ycan be the invalid character itself, such as#, or the ASCII code of the invalid character, such as35.
Input parameter data type:
xis of the VARBINARY type, andyis of the VARCHAR or BIGINT type.Return value data type: VARCHAR.
Examples:
Decode a UTF-8 encoded parameter and return the result. The statement is as follows:
SELECT from_utf8(to_utf8('hello'));The following result is returned:
+-----------------------------+ | from_utf8(to_utf8('hello')) | +-----------------------------+ | hello | +-----------------------------+Decode a parameter that is not UTF-8 encoded. The statement is as follows:
SELECT from_utf8(unhex('58BF'));The following result is returned:
+--------------------------+ | from_utf8(unhex('58BF')) | +--------------------------+ | X | +--------------------------+Decode a parameter that is not UTF-8 encoded and replace the invalid character with
#. The statement is as follows:SELECT from_utf8(unhex('58BF'), '#');The following result is returned:
+-------------------------------+ | from_utf8(unhex('58BF'), '#') | +-------------------------------+ | X# | +-------------------------------+Decodes non-UTF-8 encoded parameters and replaces invalid byte sequences with an invalid character that has an ASCII code of 35. The statement is as follows:
SELECT from_utf8(unhex('58BF'), '35');The following result is returned:
+-------------------------------+ | from_utf8(unhex('58BF'), '35') | +-------------------------------+ | X# | +-------------------------------+
HEX
hex(x)Description: Converts the parameter
xto its hexadecimal string representation.Input parameter data type: BIGINT or VARCHAR.
Return value data type: VARCHAR.
Examples:
The input value is of the BIGINT type. The statement is as follows:
SELECT hex(16);The following result is returned:
+---------+ | hex(16) | +---------+ | 10 | +---------+The input value is of the VARCHAR type. The statement is as follows:
SELECT hex('16');The following result is returned:
+-----------+ | hex('16') | +-----------+ | 3136 | +-----------+
INSTR
instr(str, substr)Description: Returns the position of the first occurrence of the substring
substrwithin the stringstr.Input parameter data type:
strandsubstrare of the VARCHAR type.Return value data type: BIGINT.
Example:
SELECT instr('foobarbar', 'bar');The following result is returned:
+---------------------------+ | instr('foobarbar', 'bar') | +---------------------------+ | 4 | +---------------------------+
LEFT
LEFT(str, len)Description: Returns the
lenleftmost characters of the stringstr.If
strorlenisnull,nullis returned.Input parameter data type:
stris of the VARCHAR type, andlenis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT LEFT('foobarbar', 5);The following result is returned:
+----------------------+ | LEFT('foobarbar', 5) | +----------------------+ | fooba | +----------------------+
LENGTH or OCTET_LENGTH
length(str)
octet_length(str)Description: Returns the length of the string
str.Input parameter data type: VARCHAR.
Return value data type: BIGINT.
Example:
SELECT length('aliyun');The following result is returned:
+------------------+ | length('aliyun') | +------------------+ | 6 | +------------------+
LIKE
expression [NOT] LIKE pattern [ESCAPE 'escape_char']Description: The
LIKEoperator matches the stringexpressionwithpattern. It returns1for a successful match and0otherwise. The parameters are described as follows:patternis a wildcard pattern. The wildcards include the following:%: Matches a string of any length._: Matches a single character.
escape_char: Escapes the%and_characters inpatternso that the%and_characters that follow the escape character are not used as wildcards.
Input parameter data type:
expressionandpatternare of the VARCHAR type.Return value data type: BIGINT.
Examples:
Statement:
SELECT 'David!' LIKE 'David_' AS result1, 'David!' NOT LIKE 'David_' AS result2, 'David!' LIKE '%D%v%' AS result3;The following result is returned:
+---------+---------+---------+ | result1 | result2 | result3 | +---------+---------+---------+ | 1 | 0 | 1 | +---------+---------+---------+Statement:
SELECT 'David_' LIKE 'David|_' ESCAPE '|';The following result is returned:
+----------------------------------+ | David_' LIKE 'David|_' ESCAPE '| | +----------------------------------+ | 1 | +----------------------------------+
LOCATE
locate(substr, str)
locate(substr, str, pos)Description: Returns the position of the first occurrence of
substrwithin the stringstr. Alternatively, it returns the position of the first occurrence ofsubstrwithinstrstarting from positionpos.If
substris not instr,0is returned.If
substrorstrisnull,nullis returned.Input parameter data type:
strandsubstrare of the VARCHAR type, andposis of the BIGINT type.Return value data type: BIGINT.
Examples:
Statement:
SELECT locate('bar', 'foobarbar');The following result is returned:
+----------------------------+ | locate('bar', 'foobarbar') | +----------------------------+ | 4 | +----------------------------+Statement:
SELECT locate('bar', 'foobarbar', 7);The following result is returned:
+-------------------------------+ | locate('bar', 'foobarbar', 7) | +-------------------------------+ | 7 | +-------------------------------+
LOWER or LCASE
lower(str)
lcase(str)Description: Converts letters in the string
strto lowercase.Return value type: VARCHAR.
Return value data type: VARCHAR.
Example:
SELECT lower('Aliyun');The following result is returned:
+-----------------+ | lower('Aliyun') | +-----------------+ | aliyun | +-----------------+
LPAD
lpad(str, len, padstr)Description: Left-pads the string
strwithpadstrto a length oflencharacters and returns the padded string.If
stris longer thanlen, the return value is shortened tolencharacters.Input parameter data type:
strandpadstrare of the VARCHAR type, andlenis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT lpad('Aliyun',9,'#');The following result is returned:
+----------------------+ | lpad('Aliyun',9,'#') | +----------------------+ | ###Aliyun | +----------------------+
LTRIM
ltrim(str)Description: Removes all leading spaces from the string
str.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Example:
SELECT ltrim(' abc');The following result is returned:
+----------------+ | ltrim(' abc') | +----------------+ | abc | +----------------+
MAKE_SET
make_set(bits, str 1, str 2,...);Description: Returns a set value, which is a string containing substrings separated by characters. The set contains strings that have the corresponding bit set.
str 1corresponds to bit0,str 2corresponds to bit1, and so on.nullvalues instr 1,str 2, and so on are not appended to the result.Input parameter data type:
bitsis of the BIGINT type, andstris of the VARCHAR type.Return value data type: VARCHAR.
Examples:
Statement:
SELECT make_set(5,'hello','nice','world');The following result is returned:
+------------------------------------+ | make_set(5,'hello','nice','world') | +------------------------------------+ | hello,world | +------------------------------------+Statement:
SELECT make_set(1 | 4,'hello','nice',NULL,'world')AS result;The following result is returned:
+--------+ | result | +--------+ | hello | +--------+
MD5_MUR
MD5_MUR(x)Description: Calculates the MD5 hash of the input parameter x and then converts the hash to a LONG hash value using the MurmurHash3 algorithm. This function has a lower collision rate than the CRC32 algorithm.
Input parameter data type: VARCHAR.
Return value data type: LONG.
Version requirement: Your cluster must be of kernel version 3.2.5 or later.
Example:
Statement:
SELECT MD5_MUR('TEST') as result;The following result is returned:
+--------------------------+ | result | +--------------------------+ | -6711128042951586494 | +--------------------------+
MID
mid(str, pos, len)Description: This function is equivalent to SUBSTR or SUBSTRING and returns a substring of length
lenfrom stringstr, starting atpos.Input parameter data type:
stris of the VARCHAR type, andposandlenare of the BIGINT type.Return value data type: VARCHAR.
Examples:
Statement:
SELECT mid('Quadratically',5,6);The following result is returned:
+--------------------------+ | mid('Quadratically',5,6) | +--------------------------+ | ratica | +--------------------------+Statement:
SELECT mid('Sakila', -5, 3);The following result is returned:
+----------------------+ | mid('Sakila', -5, 3) | +----------------------+ | aki | +----------------------+
OCT
oct(N)Description: Returns the octal string representation of the integer
N.If
Nisnull,nullis returned.Input parameter data type: BIGINT.
Return value data type: VARCHAR.
Example:
SELECT oct(12);The following result is returned:
+---------+ | oct(12) | +---------+ | 14 | +---------+
ORD
ord(x)Description: If the leftmost character of the string
xis a multibyte character, this function returns the code of that character.Input parameter data type: VARBINARY or VARCHAR.
Return value data type: LONG.
Examples:
The input parameter is of the VARCHAR type. The statement is as follows:
SELECT ord('China');The following result is returned:
+--------------+ | ord('China') | +--------------+ | 67 | +--------------+The input parameter is of the VARBINARY type. The statement is as follows:
SELECT ord(cast('China' AS varbinary));The following result is returned:
+---------------------------------+ | ord(cast('China' AS varbinary)) | +---------------------------------+ | 67 | +---------------------------------+
POSITION
position(substr IN str);Description: Returns the position of the first occurrence of the substring
substrwithin the stringstr. The position starts from1. If the substring is not found,0is returned.Input parameter data type:
substrandstrare of the VARCHAR type.Return value data type: BIGINT.
Example:
SELECT position('bar' in 'foobarbar');The following result is returned:
+--------------------------------+ | position('bar' in 'foobarbar') | +--------------------------------+ | 4 | +--------------------------------+
REPEAT
repeat(str, count);Description: Returns a string consisting of
strrepeatedcounttimes.If
count<1, an empty string is returned.If
strorcountisnull,nullis returned.Input parameter data type:
stris of the VARCHAR type, andcountis of the BIGINT type.Return value data type: VARCHAR.
Examples:
Statement:
SELECT repeat('a', 3);The following result is returned:
+----------------+ | repeat('a', 3) | +----------------+ | aaa | +----------------+Statement:
SELECT repeat('abc', null);The following result is returned:
+---------------------+ | repeat('abc', null) | +---------------------+ | NULL | +---------------------+Statement:
SELECT repeat(null, 3);The following result is returned:
+-----------------+ | repeat(null, 3) | +-----------------+ | NULL | +-----------------+
REPLACE
replace(str, from_str, to_str);Description: Replaces all occurrences of
from_strinstrwithto_str.Input parameter data type:
str,from_str, andto_strare of the VARCHAR type.Return value data type: VARCHAR.
Example:
SELECT replace('WWW.aliyun.com', 'W', 'w');The following result is returned:
+-------------------------------------+ | replace('WWW.aliyun.com', 'W', 'w') | +-------------------------------------+ | www.aliyun.com | +-------------------------------------+
REVERSE
reverse(str);Description: Returns the string
strin reverse order.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Example:
SELECT reverse('123456');The following result is returned:
+-------------------+ | reverse('123456') | +-------------------+ | 654321 | +-------------------+
RIGHT
RIGHT(str, len);Description: Returns the
lenrightmost characters of the stringstr.If
strorlenisnull,nullis returned.Input parameter data type:
stris of the VARCHAR type, andlenis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT RIGHT('abc',3);The following result is returned:
+----------------+ | RIGHT('abc',3) | +----------------+ | abc | +----------------+
RLIKE or REGEXP
expression RLIKE pattern;
expression REGEXP pattern;Description: Returns
1if the stringexpressionmatches the regular expressionpattern, and0otherwise.If
expressionorpatternisnull,nullis returned.Input parameter data type:
expressionandpatternare of the VARCHAR type.Return value data type: BOOLEAN.
Examples:
Statement:
SELECT 'Michael!' REGEXP '.*';The following result is returned:
+----------------------+ | Michael!' REGEXP '.* | +----------------------+ | 1 | +----------------------+Statement:
SELECT 'new*\n*line' REGEXP 'new\\*.\\*line';The following result is returned:
+-------------------------------------+ | new*\n*line' REGEXP 'new\\*.\\*line | +-------------------------------------+ | 0 | +-------------------------------------+Statement:
SELECT 'c' REGEXP '^[a-d]';The following result is returned:
+-------------------+ | c' REGEXP '^[a-d] | +-------------------+ | 1 | +-------------------+
RPAD
rpad(str, len, padstr)Description: Right-pads the string
strwithpadstrto a length oflencharacters and returns the padded string.If
stris longer thanlen, the return value is shortened tolencharacters.Input parameter data type:
strandpadstrare of the VARCHAR type, andlenis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT rpad('Aliyun',9,'#');The following result is returned:
+----------------------+ | rpad('Aliyun',9,'#') | +----------------------+ | Aliyun### | +----------------------+
RTRIM
rtrim(str)Description: Removes all trailing spaces from the string
str.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Example:
SELECT rtrim('barbar ');The following result is returned:
+--------------------+ | rtrim('barbar ') | +--------------------+ | barbar | +--------------------+
SPACE
space(N);Description: Returns a string consisting of a specified number of spaces.
NoteYou can use this function with the
contact()function to display the results.Input parameter data type: BIGINT.
Return value data type: VARCHAR.
Example:
SELECT concat("#", space(6), "#");The following result is returned:
+----------------------------+ | concat("#", space(6), "#") | +----------------------------+ | # # | +----------------------------+
SPLIT
split(string, delimiter)Description: Splits the
stringby thedelimiterand returns an array.Input parameter data type:
stringanddelimiterare of the VARCHAR type.Return value data type: ARRAY<varchar>.
Example:
SELECT split('1#2#3', '#'), split('#1#2#3#', '#'), split('123', '#');The following result is returned:
+---------------------+-----------------------+-------------------+ | 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: Splits the
stringby thedelimiterand returns the substring at the array indexindex. Theindexstarts from 1. If the index is greater than the number of fields, NULL is returned.Input parameter data type:
stringanddelimiterare of the VARCHAR type, andindexis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT split_part('A#B#C', '#', 2), split_part('A#B#C', '#', 4);The following result is returned:
+-----------------------------+-----------------------------+ | split_part('A#B#C', '#', 2) | split_part('A#B#C', '#', 4) | +-----------------------------+-----------------------------+ | B | NULL | +-----------------------------+-----------------------------+
SPLIT_TO_MAP
split_to_map(string, entryDelimiter, keyValueDelimiter)Description: Splits a string using an
entryDelimiterand akeyValueDelimiter, then returns amap.entryDelimitersplits the string intokey-valuepairs.keyValueDelimitersplits eachkey-valuepair into akeyand avalue.Input parameter data type:
string,entryDelimiter, andkeyValueDelimiterare of the VARCHAR type.Return value data type: MAP<varchar, varchar>.
Example:
SELECT split_to_map('k1:v1,k2:v2', ',', ':'),split_to_map('', ',', ':');The following result is returned:
+---------------------------------------+----------------------------+ | split_to_map('k1:v1,k2:v2', ',', ':') | split_to_map('', ',', ':') | +---------------------------------------+----------------------------+ | {"k1":"v1","k2":"v2"} | {} | +---------------------------------------+----------------------------+
STRCMP
strcmp(str 1, str 2);Description: If the strings
str 1andstr 2are identical, the function returns0. Ifstr 1is less thanstr 2according to the current sort order, it returns-1. Otherwise, it returns1.Input parameter data type:
str 1andstr 2are of the VARCHAR type.Return value data type: BIGINT.
Example:
SELECT strcmp('text', 'text2');The following result is returned:
+-------------------------+ | 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)andSUBSTRING(varchar str FROM pos)return a substring from positionposto the end of the string. Ifpos<0, the starting position is counted backwards from the end of the string.SUBSTRING(varchar str, bigint pos, bigint len)andSUBSTRING(varchar str FROM pos FOR len)return a substring of lengthlenfrom positionpos. Ifpos<0, the starting position is counted backwards from the end of the string.
Input parameter data type:
stris of the VARCHAR type, andposandlenare of the BIGINT type.Return value data type: VARCHAR.
Examples:
Statement:
SELECT substr('helloworld', 6);The following result is returned:
+-------------------------+ | substr('helloworld', 6) | +-------------------------+ | world | +-------------------------+Statement:
SELECT substr('helloworld' FROM 6);The following result is returned:
+-----------------------------+ | substr('helloworld' FROM 6) | +-----------------------------+ | world | +-----------------------------+Statement:
SELECT substr('helloworld', 6, 3);The following result is returned:
+----------------------------+ | substr('helloworld', 6, 3) | +----------------------------+ | wor | +----------------------------+Statement:
SELECT substr('helloworld' from 6 for 3);The following result is returned:
+-----------------------------------+ | substr('helloworld' FROM 6 FOR 3) | +-----------------------------------+ | wor | +-----------------------------------+
SUBSTRING_INDEX
substring_index(str, delim, count)Description: Returns a substring from the string
strthat is before or after the count-th occurrence of the delimiterdelim.If
count>0, the function returns everything to the left of the count-th occurrence of the delimiter (delim). Counting starts from the left.If
count<0, everything to the right of the count-th delimiter (delim) is returned, with the count starting from the right.The
SUBSTRING_INDEXfunction performs a case-sensitive search fordelim.Input parameter data type:
stranddelimare of the VARCHAR type, andcountis of the BIGINT type.Return value data type: VARCHAR.
Example:
SELECT substring_index('www.aliyun.com', '.', 2);The following result is returned:
+-------------------------------------------+ | substring_index('www.aliyun.com', '.', 2) | +-------------------------------------------+ | www.aliyun | +-------------------------------------------+
TO_BASE64
to_base64(x)Description: Returns the Base64-encoded form of the parameter
x.Input parameter data type: VARBINARY or VARCHAR.
Return value data type: VARCHAR.
Examples:
The input parameter is of the VARCHAR type. The statement is as follows:
SELECT to_base64('China');The following result is returned:
+--------------------+ | to_base64('China') | +--------------------+ | Q2hpbmE= | +--------------------+The input parameter is of the VARBINARY type. The statement is as follows:
SELECT to_base64(cast('China' AS varbinary));The following result is returned:
+---------------------------------------+ | to_base64(cast('China' AS varbinary)) | +---------------------------------------+ | Q2hpbmE= | +---------------------------------------+
TO_UTF8
to_utf8(x)Description: Returns the UTF-8 encoded form of the parameter
x.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Example:
SELECT from_utf8(to_utf8('China'));The following result is returned:
+-----------------------------+ | from_utf8(to_utf8('China')) | +-----------------------------+ | China | +-----------------------------+
TRIM
trim([remstr FROM] str)
trim([{BOTH | LEADING | TRAILING} [remstr] FROM] str)Description: Trims the string
strby removing leading and trailing spaces, or by removing characters that match the optionalremstrstring.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Examples:
Statement:
SELECT trim(' bar ');The following result is returned:
+------------------+ | trim(' bar ') | +------------------+ | bar | +------------------+Statement:
SELECT trim(BOTH 'x' FROM 'xxxbarxxx');The following result is returned:
+---------------------------------+ | trim(BOTH 'x' FROM 'xxxbarxxx') | +---------------------------------+ | bar | +---------------------------------+Statement:
SELECT trim(LEADING 'x' FROM 'xxxbarxxx');The following result is returned:
+------------------------------------+ | trim(LEADING 'x' FROM 'xxxbarxxx') | +------------------------------------+ | barxxx | +------------------------------------+Statement:
SELECT trim(TRAILING 'x' from 'xxxbarxxx');The following result is returned:
+-------------------------------------+ | trim(TRAILING 'x' from 'xxxbarxxx') | +-------------------------------------+ | xxxbar | +-------------------------------------+
UPPER or UCASE
upper(str)
ucase(str)Description: Converts letters in the string
strto uppercase.Input parameter data type: VARCHAR.
Return value data type: VARCHAR.
Example:
SELECT upper('Aliyun');The following result is returned:
+-----------------+ | upper('Aliyun') | +-----------------+ | ALIYUN |
UNHEX
unhex(x);Description: Interprets each pair of hexadecimal digits in the parameter
xas a number and converts it to the character that the number represents.Input parameter data type: VARBINARY or VARCHAR.
Return value data type: VARBINARY.
NoteThe decoded return value is of the VARBINARY type. To convert the result to the VARCHAR type, use one of the following methods:
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, you can use the
FROM_UTF8function to convert data types. For more information, see FROM_UTF8.
If the input value for UNHEX contains any non-hexadecimal digits, NULL is returned.
Examples:
The input parameter is of the VARCHAR type. The statement is as follows:
SELECT unhex(hex('China'));The following result is returned:
+------------------------------------------+ | unhex(hex('China')) | +------------------------------------------+ | China | +------------------------------------------+NoteThe return value of the preceding statement is of the VARBINARY type. To obtain the original VARCHAR value, run the following statement:
SELECT cast(unhex(hex('China')) AS varchar);The following result is returned:
+--------------------------------------+ | cast(unhex(hex('China')) AS varchar) | +--------------------------------------+ | China | +--------------------------------------+The input parameter is of the VARBINARY type. The statement is as follows:
SELECT unhex(cast(hex('China') AS varbinary));The following result is returned:
+------------------------------------------+ | unhex(cast(hex('China') AS varbinary)) | +------------------------------------------+ | China | +------------------------------------------+