String Functions
Overview
String functions are used to perform operations on data of the VARCHAR or STRING type, including concatenation, truncation, replacement, case conversion, trimming, length calculation, padding, and pattern matching.
For more information, see String Functions.
Application Scenarios
- Data cleansing: Uses TRIM to remove spaces and use UPPER or LOWER to convert the case to uppercase or lowercase.
- String concatenation: Uses CONCAT or CONCAT_WS to concatenate multiple fields.
- Data masking: Uses SUBSTRING and REPLACE to mask fields.
- Pattern replacement: Uses REGEXP_REPLACE to perform regular expression replacement.
- Formatted output: Uses LPAD or RPAD to align fields and uses FORMAT_NUMBER to format numbers.
- Pattern matching: Uses LIKE for simple pattern matching and REGEXP for regular expression matching.
Notes and Constraints
- If any parameter in CONCAT is NULL, the result is NULL. CONCAT_WS skips NULL parameters.
- The start position of SUBSTRING is 1. The value 0 is equivalent to 1.
- REGEXP_REPLACE uses the Java regular expression syntax.
- When string1 is specified in the TRIM function, only single characters that exactly match string1 are removed. This is not a substring deletion operation.
- If len is less than the original length of the string, LPAD or RPAD truncates the string to the length of len.
Syntax Structure
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| string1 || string2 | STRING | String concatenation. Concatenates two strings. The string1 and string2 are concatenated end-to-end to form a new string. Both string1 and string2 must be of the STRING type. If either operand is NULL, NULL is returned. | SELECT 'hello' \|\| ' ' \|\| 'world'; -- 'hello world' |
| CHAR_LENGTH(string) | INT | Returns the number of characters in a string. The string must be of the STRING type. This function is equivalent to CHARACTER_LENGTH. A multi-byte character (such as Chinese) is counted as one character. If the string is NULL, NULL is returned. | SELECT CHAR_LENGTH('hello'); -- 5 |
| CHARACTER_LENGTH(string) | INT | Returns the number of characters in a string. This function is equivalent to CHAR_LENGTH, which is a SQL standard alias. The string must be of the STRING type. If the string is NULL, NULL is returned. | SELECT CHARACTER_LENGTH('Hello'); -- 2 |
| UPPER(string) | STRING | Converts a string to uppercase. All lowercase letters in the string are converted to uppercase letters, and non-letter characters remain unchanged. The string must be of the STRING type. If the string is NULL, NULL is returned. | SELECT UPPER('hello'); -- 'HELLO' |
| LOWER(string) | STRING | Converts a string to lowercase. All uppercase letters in the string are converted to lowercase letters, and non-letter characters remain unchanged. The string must be of the STRING type. If the string is NULL, NULL is returned. | SELECT LOWER('HELLO'); -- 'hello' |
| INITCAP(string) | STRING | Converts the first letter of each word in a string to uppercase and the other letters to lowercase. Words are separated by non-letter characters. The string must be of the STRING type. If the string is NULL, NULL is returned. | SELECT INITCAP('hello world'); -- 'Hello World' |
| TRIM([ BOTH | LEADING | TRAILING ] string1 FROM string2) | STRING | Removes characters that match string1 from the beginning (LEADING), end (TRAILING), or both ends (BOTH, default) of string2. If string1 and BOTH are omitted, spaces at both ends are removed by default. Both string1 and string2 must be of the STRING type. If any parameter is NULL, NULL is returned. | SELECT TRIM(BOTH 'x' FROM 'xxhelloxx'); -- 'hello' |
| LTRIM(string) | STRING | Removes leading spaces from the left side of a string. The string must be of the STRING type. If the string is NULL, NULL is returned. | SELECT LTRIM(' hello'); -- 'hello' |
| RTRIM(string) | STRING | Removes trailing spaces. All space characters on the right side of the string are removed. The string must be of the STRING type. If the string is NULL, NULL is returned. | SELECT RTRIM('hello '); -- 'hello' |
| SUBSTRING(string FROM pos [FOR len]) | STRING | Extracts a substring (SQL standard syntax). A substring of the specified length is extracted from the string starting at the position specified by pos. The value of pos starts from 1. The value 0 is equivalent to 1. If FOR len is omitted, the substring is extracted to the end of the string. If pos is a negative number, the substring is extracted from the end of the string. The string must be of the STRING type, and pos must be of the INT type. If the string is NULL, NULL is returned. | SELECT SUBSTRING('hello world' FROM 1 FOR 5); -- 'hello' |
| SUBSTRING(string, pos [, len]) | STRING | Extracts a substring (comma syntax). This function is semantically equivalent to SUBSTRING(string FROM pos [FOR len]), but the syntax is different. The value of pos starts from 1. The value 0 is equivalent to 1. If len is omitted, the substring is extracted to the end of the string. The string must be of the STRING type, and pos must be of the INT type. If the string is NULL, NULL is returned. | SELECT SUBSTRING('hello world', 7); -- 'world' |
| CONCAT(string1, string2, ...) | STRING | Concatenates strings. All parameters are concatenated into a single string in sequence. All parameters must be of the STRING type. Two or more parameters are supported. The key difference from the || operator is that if any parameter is NULL, the entire result is NULL. | SELECT CONCAT('hello', ' ', 'world'); -- 'hello world' |
| CONCAT_WS(separator, string1, ...) | STRING | Concatenates all non-null string parameters with a separator. The separator and string parameters must be of the STRING type. The key difference from CONCAT is that NULL parameters are skipped instead of being returned. If the separator is NULL, NULL is returned. | SELECT CONCAT_WS('-', '2024', '01', '15'); -- '2024-01-15' |
| REPLACE(string, old, new) | STRING | Replaces all occurrences of the old substring in string with the new substring. The replacement is case-sensitive. If the old substring is an empty string, the replacement is not performed. The string, old, and new parameters must be of the STRING type. If any parameter is NULL, NULL is returned. | SELECT REPLACE('hello world', 'world', 'Flink'); -- 'hello Flink' |
| REGEXP_REPLACE(string, regex, replacement) | STRING | Replaces all substrings that match the regular expression in string with the replacement. The Java regular expression syntax is used. The replacement can use 1, 1 and 2, and so on to reference capture groups. The string, regex, and replacement parameters must be of the STRING type. If any parameter is NULL, NULL is returned. | SELECT REGEXP_REPLACE('abc123', '[0-9]', ''); -- 'abc**' |
| REGEXP_EXTRACT(string, regex [, group]) | STRING | Extracts a substring that matches the regular expression from a string. group specifies the ID of the capture group to be returned. The default value is 0, indicating that the entire match is returned. string and regex must be of the STRING type, and group must be of the INT type. If no match is found, NULL is returned. If any parameter is NULL, NULL is returned. | SELECT REGEXP_EXTRACT('abc123def', '([a-z]+)([0-9]+)', 2); -- '123' |
| POSITION(string1 IN string2) | INT | Returns the starting position where string1 first appears in string2, starting from 1. If string1 is not found, 0 is returned. string1 and string2 must be of the STRING type. If any parameter is NULL, NULL is returned. | SELECT POSITION('world' IN 'hello world'); -- 7 |
| LPAD(string, len, pad) | STRING | Pads the left side of a string with the pad string until the total length reaches len. If the length of the string exceeds len, the string is truncated to len from the right. string and pad must be of the STRING type, and len must be of the INT type. If any parameter is NULL, NULL is returned. | SELECT LPAD('5', 3, '0'); -- '005' |
| RPAD(string, len, pad) | STRING | Pads the right side of a string with the pad string until the total length reaches len. If the length of the string exceeds len, the string is truncated to len from the right. string and pad must be of the STRING type, and len must be of the INT type. If any parameter is NULL, NULL is returned. | SELECT RPAD('5', 3, '0'); -- '500' |
| OVERLAY(string1 PLACING string2 FROM pos [FOR len]) | STRING | Overwrites a substring. It overwrites len characters in string1 starting from position pos. The value of pos starts from 1. If FOR len is omitted, the length of the string to be overwritten is the length of string2. string1 and string2 must be of the STRING type, and pos and len must be of the INT type. If any parameter is NULL, NULL is returned. | SELECT OVERLAY('hello world' PLACING 'FLINK' FROM 7); -- 'hello FLINK' |
| REVERSE(string) | STRING | Reverses a string. The order of characters in string is reversed. string must be of the STRING type. If string is NULL, NULL is returned. | SELECT REVERSE('hello'); -- 'olleh' |
| REPEAT(string, integer) | STRING | Repeats a string. The string is repeated integer times and then concatenated into a new string. string must be of the STRING type, and integer must be of the INT type. If integer is 0, an empty string is returned. If integer is a negative number, NULL is returned. If string is NULL, NULL is returned. | SELECT REPEAT('ab', 3); -- 'ababab' |
| SPACE(integer) | STRING | Generates a string of spaces. A string consisting of integer spaces is returned. integer must be of the INT type. If integer is 0, an empty string is returned. If integer is a negative number, NULL is returned. | SELECT SPACE(3); -- ' ' |
| LEFT(string, integer) | STRING | Left truncation. Returns the leftmost integer characters of a string. The string must be of the STRING type, and the integer must be of the INT type. If the integer is 0, an empty string is returned. If the integer is a negative number, NULL is returned. If the string is NULL, NULL is returned. | SELECT LEFT('hello world', 5); -- 'hello' |
| RIGHT(string, integer) | STRING | Right truncation. Returns the rightmost integer characters of a string. The string must be of the STRING type, and the integer must be of the INT type. If the integer is 0, an empty string is returned. If the integer is a negative number, NULL is returned. If the string is NULL, NULL is returned. | SELECT RIGHT('hello world', 5); -- 'world' |
| FORMAT_NUMBER(numeric, decimal_places) | STRING | Formats a number. The numeric value is formatted into a string with thousands separators, and decimal_places decimal places are retained. The numeric value must be of a numeric type, and decimal_places must be of the INT type. If either parameter is NULL, NULL is returned. | SELECT FORMAT_NUMBER(12345.6789, 2); -- '12,345.68' |
| LIKE(string1, string2) | BOOLEAN | Pattern matching. Determines whether string1 matches the pattern string2. The percent sign (%) matches any sequence of characters (including an empty sequence), and the underscore (_) matches exactly one character. To match a wildcard character itself, use the ESCAPE clause for escaping. Both string1 and string2 must be of the STRING type. If either parameter is NULL, NULL is returned. | SELECT LIKE('hello', 'h%'); -- TRUE |
| REGEXP(string1, string2) | BOOLEAN | Performs regular expression matching. Determines whether string1 matches the regular expression string2. The Java regular expression syntax is used. Both string1 and string2 must be of the STRING type. If either parameter is NULL, NULL is returned. | SELECT REGEXP('abc123', '[a-z]+[0-9]+'); -- TRUE |
| URL_DECODE | VARCHAR | The URL_DECODE function is used to convert an input string in application/x-www-form-urlencoded MIME format to a standard string. | For details, see URL_DECODE. |
| URL_ENCODE | VARCHAR | The URL_ENCODE function is used to encode an input string in application/x-www-form-urlencoded MIME format and return the encoded string. | For details, see URL_ENCODE. |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot