String Functions
The common character string functions of DLI are as follows:
Operator |
Returned Data Type |
Description |
---|---|---|
VARCHAR |
Concatenates two strings. |
|
INT |
Returns the number of characters in a string. |
|
INT |
Returns the number of characters in a string. |
|
VARCHAR |
Concatenates two or more string values to form a new string. If the value of any parameter is NULL, skip this parameter. |
|
VARCHAR |
Concatenates each parameter value and the separator specified by the first parameter separator to form a new string. The length and type of the new string depend on the input value. |
|
INT |
Returns the absolute value of HASH_CODE() of a string. In addition to string, int, bigint, float, and double are also supported. |
|
VARCHAR |
Returns a string whose first letter is in uppercase and the other letters in lowercase. Words are sequences of alphanumeric characters separated by non-alphanumeric characters. |
|
BOOLEAN |
Checks whether a string contains only letters. |
|
BOOLEAN |
Checks whether a string contains only digits. |
|
BOOLEAN |
Checks whether a string is numeric. |
|
BOOLEAN |
Checks whether a string is a valid URL. |
|
VARCHAR |
Obtains the value of a specified path in a JSON string. |
|
VARCHAR |
Obtains the value of a key in a key-value pair string. |
|
VARCHAR |
Returns a string of lowercase characters. |
|
VARCHAR |
Concatenates the pad string to the left of the str string until the length of the new string reaches the specified length len. |
|
VARCHAR |
Returns the MD5 value of a string. If the parameter is an empty string (that is, the parameter is "), an empty string is returned. |
|
VARCHAR |
Replaces the substring of x with y. Replace length+1 characters starting from start_position. |
|
INT |
Returns the position of the first occurrence of the target string x in the queried string y. If the target string x does not exist in the queried string y, 0 is returned. |
|
VARCHAR |
Replaces all str2 in the str1 string with str3.
|
|
VARCHAR |
Concatenates the pad string to the right of the str string until the length of the new string reaches the specified length len. |
|
STRING |
Returns the SHA1 value of the expr string. |
|
STRING |
Returns the SHA256 value of the expr string. |
|
ARRAY[STRING] |
Separates the value string as string arrays by using the delimiter. |
|
VARCHAR |
Returns the substring starting from a fixed position of A. The start position starts from 1. |
|
STRING |
Removes A at the start position, or end position, or both the start and end positions from B. By default, string expressions A at both the start and end positions are removed. |
|
VARCHAR |
Returns a string converted to uppercase characters. |
CONCAT
- Function
Concatenates two or more string values to form a new string. If the value of any parameter is NULL, skip this parameter.
- Syntax
VARCHAR CONCAT(VARCHAR var1, VARCHAR var2, ...)
- Parameter description
- var1: character string
- var2: character string
- Example
- Test statement
SELECT CONCAT("abc", "def", "ghi", "jkl");
- Test result
"abcdefghijkl"
- Test statement
CONCAT_WS
- Function
Concatenates each parameter value and the separator specified by the first parameter separator to form a new string. The length and type of the new string depend on the input value.
If the value of separator is null, separator is combined with an empty string. If other parameters are set to null, the parameters whose values are null are skipped during combination.
- Syntax
VARCHAR CONCAT_WS(VARCHAR separator, VARCHAR var1, VARCHAR var2, ...)
- Parameter description
- separator: separator.
- var1: character string
- var2: character string
- Example
- Test statement
SELECT CONCAT_WS("-", "abc", "def", "ghi", "jkl");
- Test result
"abc-def-ghi-jkl"
- Test statement
INITCAP
- Function
Return the string whose first letter is in uppercase and the other letters in lowercase. Strings are sequences of alphanumeric characters separated by non-alphanumeric characters.
- Syntax
VARCHAR INITCAP(a)
- Parameter description
- a: character string.
- Example
- Test statement
SELECT INITCAP(var1)as aa FROM T1;
- Test data and result
Table 4 Test data and result Test Data (var1)
Test Result (aa)
aBCde
Abcde
- Test statement
IS_ALPHA
- Function
- Syntax
BOOLEAN IS_ALPHA(VARCHAR content)
- Parameter description
- content: Enter a character string.
- Example
- Test statement
SELECT IS_ALPHA(content) AS case_result FROM T1;
- Test data and results
Table 5 Test data and results Test Data (content)
Test Result (case_result)
Abc
true
abc1#$
false
null
false
Empty string
false
- Test statement
IS_DIGITS
- Function
- Syntax
BOOLEAN IS_DIGITS(VARCHAR content)
- Parameter description
- content: Enter a character string.
- Example
- Test statement
SELECT IS_DIGITS(content) AS case_result FROM T1;
- Test data and results
Table 6 Test data and results Test Data (content)
Test Result (case_result)
78
true
78.0
false
78a
false
null
false
Empty string
false
- Test statement
IS_NUMBER
- Function
This function is used to check whether a character string is a numeric string.
- Syntax
BOOLEAN IS_NUMBER(VARCHAR content)
- Parameter description
- content: Enter a character string.
- Example
- Test statement
SELECT IS_NUMBER(content) AS case_result FROM T1;
- Test data and results
Table 7 Test data and results Test Data (content)
Test Result (case_result)
78
true
78.0
true
78a
false
null
false
Empty string
false
- Test statement
IS_URL
- Function
This function is used to check whether a character string is a valid URL.
- Syntax
BOOLEAN IS_URL(VARCHAR content)
- Parameter description
- content: Enter a character string.
- Example
- Test statement
SELECT IS_URL(content) AS case_result FROM T1;
- Test data and results
Table 8 Test data and results Test Data (content)
Test Result (case_result)
https://www.testweb.com
true
https://www.testweb.com:443
true
www.testweb.com:443
false
null
false
Empty string
false
- Test statement
JSON_VALUE
- Function
Obtains the value of a specified path in a JSON character string.
- Syntax
VARCHAR JSON_VALUE(VARCHAR content, VARCHAR path)
- Parameter description
- content: Enter a character string.
- path: path to be obtained.
- Example
- Test statement
SELECT JSON_VALUE(content, path) AS case_result FROM T1;
- Test data and results
Table 9 Test data and results Test Data (content and path)
Test Result (case_result)
{ "a1":"v1","a2":7,"a3":8.0,"a4": {"a41":"v41","a42": ["v1","v2"]}}
$
{ "a1":"v1","a2":7,"a3":8.0,"a4": {"a41":"v41","a42": ["v1","v2"]}}
{ "a1":"v1","a2":7,"a3":8.0,"a4": {"a41":"v41","a42": ["v1","v2"]}}
$.a1
v1
{ "a1":"v1","a2":7,"a3":8.0,"a4": {"a41":"v41","a42": ["v1","v2"]}}
$.a4
{"a41":"v41","a42": ["v1","v2"]}
{ "a1":"v1","a2":7,"a3":8.0,"a4": {"a41":"v41","a42": ["v1","v2"]}}
$.a4.a42
["v1","v2"]
{ "a1":"v1","a2":7,"a3":8.0,"a4": {"a41":"v41","a42": ["v1","v2"]}}
$.a4.a42[0]
v1
- Test statement
KEY_VALUE
- Function
This function is used to obtain the value of a key in a key-value pair string.
- Syntax
VARCHAR KEY_VALUE(VARCHAR content, VARCHAR split1, VARCHAR split2, VARCHAR key_name)
- Parameter description
- content: Enter a character string.
- split1: separator of multiple key-value pairs.
- split2: separator between the key and value.
- key_name: name of the key to be obtained.
- Example
- Test statement
SELECT KEY_VALUE(content, split1, split2, key_name) AS case_result FROM T1;
- Test data and results
Table 10 Test data and results Test Data (content, split1, split2, and key_name)
Test Result (case_result)
k1=v1;k2=v2
;
=
k1
v1
null
;
=
k1
null
k1=v1;k2=v2
null
=
k1
null
- Test statement
LPAD
- Function
Concatenates the pad string to the left of the str string until the length of the new string reaches the specified length len.
- Syntax
VARCHAR LPAD(VARCHAR str, INT len, VARCHAR pad)
- Parameter description
- str: character string before concatenation.
- len: length of the concatenated character string.
- pad: character string to be concatenated.
- If any parameter is null, null is returned.
- If the value of len is a negative number, value null is returned.
- If the value of len is less than the length of str, the first chunk of str characters in len length is returned.
- Example
- Test statement
SELECT LPAD("adc", 2, "hello"), LPAD("adc", -1, "hello"), LPAD("adc", 10, "hello");
- Test result
"ad",,"helloheadc"
- Test statement
MD5
- Function
Returns the MD5 value of a string. If the parameter is an empty string (that is, the parameter is "), an empty string is returned.
- Syntax
VARCHAR MD5(VARCHAR str)
- Parameter description
- str: character string
- Example
- Test statement
SELECT MD5("abc");
- Test result
"900150983cd24fb0d6963f7d28e17f72"
- Test statement
OVERLAY
- Function
Replaces the substring of x with y. Replaces length+1 characters starting from start_position.
- Syntax
VARCHAR OVERLAY ( (VARCHAR x PLACING VARCHAR y FROM INT start_position [ FOR INT length ]) )
- Parameter description
- x: character string
- y: character string.
- start_position: start position.
- length (optional): indicates the character length.
- Example
- Test statement
OVERLAY('abcdefg' PLACING 'xyz' FROM 2 FOR 2) AS result FROM T1;
- Test result
Table 12 Test result result
axyzdefg
- Test statement
POSITION
- Function
Returns the position of the first occurrence of the target string x in the queried string y. If the target character string x does not exist in the queried character string y, 0 is returned.
- Syntax
INTEGER POSITION(x IN y)
- Parameter description
- x: character string
- y: character string.
- Example
- Test statement
POSITION('in' IN 'chin') AS result FROM T1;
- Test result
Table 13 Test result result
3
- Test statement
REPLACE
- Function
The character string replacement function is used to replace all str2 in the str1 string with str3.
- Syntax
VARCHAR REPLACE(VARCHAR str1, VARCHAR str2, VARCHAR str3)
- Parameter description
- str1: original character.
- str2: target character.
- str3: replacement character.
- Example
- Test statement
SELECT replace( "hello world hello world hello world", "world", "hello" );
- Test result
"hello hello hello hello hello hello"
- Test statement
RPAD
- Function
Concatenates the pad string to the right of the str string until the length of the new string reaches the specified length len.
- If any parameter is null, null is returned.
- If the value of len is a negative number, value null is returned.
- The value of pad is an empty string. If the value of len is less than the length of str, the string whose length is the same as the length of str is returned.
- Syntax
VARCHAR RPAD(VARCHAR str, INT len, VARCHAR pad)
- Parameter description
- str: start character string.
- len: indicates the length of the new character string.
- pad: character string that needs to be added repeatedly.
- Example
- Test statement
SELECT RPAD("adc", 2, "hello"), RPAD("adc", -1, "hello"), RPAD("adc", 10, "hello");
- Test result
"ad",,"adchellohe"
- Test statement
STRING_TO_ARRAY
- Function
Separates the value string as character string arrays by using the delimiter.
delimiter uses the Java regular expression. If special characters are used, they need to be escaped.
- Syntax
ARRAY[String] STRING_TO_ARRAY(STRING value, VARCHAR delimiter)
- Parameter description
- value: character string.
- delimiter: specifies the delimiter.
- Example
- Test statement
SELECT string_to_array("127.0.0.1", "\\."), string_to_array("red-black-white-blue", "-");
- Test result
[127,0,0,1],[red,black,white,blue]
- Test statement
SUBSTRING
- Function
Returns the substring that starts from a fixed position of A. The start position starts from 1.
- If len is not specified, the substring from the start position to the end of the string is truncated.
- If len is specified, the substring starting from the position specified by start is truncated. The length is specified by len.
The value of start starts from 1. If the value is 0, it is regarded as 1. If the value of start is a negative number, the position is calculated from the end of the character string in reverse order.
- Syntax
VARCHAR SUBSTRING(STRING A FROM INT start)
Or
VARCHAR SUBSTRING(STRING A FROM INT start FOR INT len)
- Parameter description
- A: specified character string.
- start: start position for truncating the character string A.
- len: intercepted length.
- Example
- Test statement 1
SELECT SUBSTRING("123456" FROM 2);
- Test result 1
"23456"
- Test statement 2
SELECT SUBSTRING("123456" FROM 2 FOR 4);
- Test result 2
"2345"
- Test statement 1
TRIM
- Function
Remove A at the start position, or end position, or both the start and end positions from B. By default, string expressions A at both the start and end positions are removed.
- Syntax
STRING TRIM( { BOTH | LEADING | TRAILING } STRING a FROM STRING b)
- Parameter description
- a: character string.
- b: character string.
- Example
- Test statement
SELECT TRIM(BOTH " " FROM " hello world ");
- Test result
"hello world"
- Test statement
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.