Updated on 2025-08-14 GMT+08:00

String Functions

This section describes string functions, including their syntax, parameters, and usage examples.

Function List

Table 1 String functions

Function

Description

lpad

Pads the beginning of a string with specified characters until it reaches the specified length, and then returns the result string. If you do not specify padding characters, space characters are used for padding.

rpad

Pads the end of a string with specified characters until it reaches the specified length, and then returns the result string. If you do not specify padding characters, space characters are used for padding.

replace

Replaces all occurrences of a matched substring in the original string with a specified string. If no replacement string is specified, all matched substrings are removed from the original string.

reverse

Reverses the order of characters in a string.

repeat

Repeats a string a specified number of times.

contains_string

Checks whether a string contains a specified substring.

icontains_string

Checks whether a string contains a specified substring, regardless of letter case.

textcat

Concatenates two strings and returns the concatenated result.

btrim

Removes spaces from both sides of a string.

parse_long

Converts a string to a long integer using a specified radix (base). If no radix is provided, the default radix of 10 (decimal) is used.

split

Splits a string using a specified delimiter and returns an array of substrings. You can set limit to limit the number of resulting substrings.

split_part

Splits a string using a specified delimiter and returns the substring at a specified position. If the position exceeds the array length, an empty string is returned.

split_to_map

Splits a string using the first delimiter, then splits the string further using the second delimiter, and returns the result.

string_format

Returns a string formatted using Java's String.format method.

strpos

Returns the position of the first occurrence of the target substring in a string. It returns 0 if the target substring is not found.

substr

Extracts a substring based on the start position and length. If the length is not specified, it returns the entire string starting from the start position. It is equivalent to the substring function.

substring

Extracts a substring based on the start position and length. If the length is not specified, it returns the entire string starting from the start position.

length

Returns the length of the input characters (for a string expression) or the byte length (for a non-string expression). The byte length is at least the smallest integer obtained by dividing the number of digits by 8.

char_length

Returns the length of the input characters (for a string expression) or the byte length (for a non-string expression). The byte length is at least the smallest integer obtained by dividing the number of digits by 8. This function is equivalent to the length function.

character_length

Equivalent to the length function.

strlen

Equivalent to the length function.

levenshtein_distance

Calculates the minimum edit distance between strings str1 and str2.

normalize

Formats a string.

to_utf8

Converts a string to the UTF-8 encoding format.

chr

Converts an ASCII value to characters.

concat

Concatenates multiple parameters into a string.

lpad

This function pads the beginning of a string with specified characters until it reaches the specified length, and then returns the result string. If you do not specify padding characters, space characters are used for padding.

Syntax: lpad(str, len, lpadStr)

Table 2 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

len

Length of the result string.

  • If the original string is shorter than len, it is padded with specified characters at the beginning.
  • If the original string is longer than len, the return value is shortened to len characters.

Integer

Yes

lpadStr

Padding characters.

String

No

Return value type: string

Example: SELECT LPAD('hello world', 10), LPAD('hello', 10, 'e')

Table 3 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

LPAD('hello world', 10)

hello worl

Scenario 2

LPAD('hello', 10, 'e')

eeeeehello

rpad

This function pads the end of a string with specified characters until it reaches the specified length, and then returns the result string. If you do not specify padding characters, space characters are used for padding.

Syntax: rpad(str, len, rpadStr)

Table 4 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

len

Length of the result string.

  • If the original string is shorter than len, it is padded with specified characters at the end.
  • If the original string is longer than len, the return value is shortened to len characters.

Integer

Yes

rpadStr

Padding characters.

String

No

Return value type: string

Example: SELECT RPAD('hello world', 10), RPAD('hello', 10, 'e')

Table 5 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

RPAD('hello world', 10)

hello worl

Scenario 2

RPAD('hello', 10, 'e')

helloeeeee

replace

This function replaces all occurrences of a matched substring in the original string with a specified string. If no replacement string is specified, all matched substrings are removed from the original string.

Syntax: replace(str, subStr, replaceStr)

Table 6 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

subStr

Target substring.

String

Yes

replaceStr

String for replacement.

String

No

Return value type: string

Example: SELECT REPLACE('hello world', 'o'), REPLACE('hello world', 'w', 'new w')

Table 7 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

REPLACE('hello world', 'o')

hell wrld

Scenario 2

REPLACE('hello world', 'w', 'new w')

hello new world

reverse

This function reverses the order of characters in a string.

Syntax: reverse(str)

Table 8 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

Return value type: string

Example: SELECT REVERSE('hello world')

Table 9 Query and analysis results

Type

Query Statement

Returned Result

Scenario

REVERSE('hello world')

dlrow olleh

repeat

This function repeats a string a specified number of times.

Syntax: repeat(str, num)

Table 10 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

num

Number of repetitions.

Integer

Yes

Return value type: string

Example: SELECT REPEAT('hello world', 2), REPEAT('hello world', 0)

Table 11 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

REPEAT('hello world', 2)

hello worldhello world

Scenario 2

REPEAT('hello world', 0)

  

contains_string

This function checks whether a string contains a specified substring.

Syntax: contains_string(str, containsStr)

Table 12 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

containsStr

Specified string.

String

Yes

Return value type: Boolean

Example: SELECT CONTAINS_STRING('hello world', 'llo')

Table 13 Query and analysis results

Type

Query Statement

Returned Result

Scenario

CONTAINS_STRING('hello world', 'llo')

true

icontains_string

This function checks whether a string contains a specified substring, regardless of letter case.

Syntax: icontains_string(str, containsStr)

Table 14 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

containsStr

Specified string.

String

Yes

Return value type: Boolean

Example: SELECT ICONTAINS_STRING('hello world', 'llo')

Table 15 Query and analysis results

Type

Query Statement

Returned Result

Scenario

ICONTAINS_STRING('hello world', 'llo')

true

textcat

This function concatenates two strings and returns the concatenated result.

Syntax: textcat(str1, str2)

Table 16 Parameter description

Parameter

Description

Type

Mandatory

str1

String 1 to be concatenated.

String

Yes

str2

String 2 to be concatenated.

String

Yes

Return value type: string

Example: SELECT TEXTCAT('hello ', 'world')

Table 17 Query and analysis results

Type

Query Statement

Returned Result

Scenario

TEXTCAT('hello ', 'world')

hello world

btrim

This function removes spaces from both sides of a string.

Syntax: btrim(str)

Table 18 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

Return value type: string

Example: SELECT BTRIM(' hello world ')

Table 19 Query and analysis results

Type

Query Statement

Returned Result

Scenario

BTRIM(' hello world ')

hello world

parse_long

This function converts a string to a long integer using a specified radix (base). If no radix is provided, the default radix of 10 (decimal) is used.

Syntax: parse_long(str, radix)

Table 20 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

radix

Base.

Integer

No

Return value type: long

Example: SELECT PARSE_LONG('-1234', 8), PARSE_LONG('1234')

Table 21 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

PARSE_LONG('-1234', 8)

-668

Scenario 2

PARSE_LONG('1234')

1234

split

This function splits a string using a specified delimiter and returns an array of substrings. You can set limit to limit the number of resulting substrings.

Syntax: split(str, splitStr, limit)

Table 22 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

splitStr

Delimiter.

String

Yes

limit

Base.

Integer

No

Return value type: ARRAYS<STRING>

Example: SELECT SPLIT('helloworld', 'o'), SPLIT('helloworld', 'o', 2)

Table 23 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

SPLIT('helloworld','o' )

["hell","w","rld"]

Scenario 2

SPLIT('helloworld', 'o', 2)

["hell","world"]

split_part

This function splits a string using a specified delimiter and returns the substring at a specified position. If the position exceeds the array length, an empty string is returned.

Syntax: split_part(str, splitStr, position)

Table 24 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

splitStr

Delimiter.

String

Yes

position

Position of the string to be returned.

Integer

Yes

Return value type: string

Example: SELECT SPLIT_PART('helloworld', 'o', 2), SPLIT_PART('helloworld', 'o', 4)

Table 25 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

SPLIT_PART('helloworld','o', 2)

w

Scenario 2

SPLIT_PART('helloworld', 'o', 4)

  

split_to_map

This function splits a string using the first delimiter, then splits the string further using the second delimiter, and returns the result.

Syntax: split_to_map(str, splitStr1, splitStr2)

Table 26 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

splitStr1

Delimiter 1.

String

Yes

splitStr2

Delimiter 2.

String

Yes

Return value type: array

Example: SELECT SPLIT_TO_MAP('upstream_response_time:123, request_time:456', ',', ':')

Table 27 Query and analysis results

Type

Query Statement

Returned Result

Scenario

SPLIT_TO_MAP('upstream_response_time:123, request_time:456', ',', ':')

{" request_time":"456","upstream_response_time":"123"}

string_format

This function returns a string formatted using Java's String.format method.

Syntax: string_format(str, Object... args)

Table 28 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

args

Output time format.

String/Integer/Long/Boolean/Double

Yes

Return value type: string

Example: SELECT STRING_FORMAT('My name is %s and I am %d years old.', 'name', age)

Table 29 Query and analysis results

Type

Query Statement

Returned Result

Scenario

STRING_FORMAT('My name is %s and I am %d years old.', 'Tom', 25)

My name is Tom and I am 25 years old.

  • %d: outputs an integer.
  • %f: outputs a floating point number.
  • %s: outputs a string.
  • %n: outputs a newline character.
  • %c: outputs characters.
  • %b: outputs the Boolean type.
  • %e: outputs the exponential type.
  • %tc: outputs date and time information.
  • %tF: The output format is YYYY-MM-DD.
  • %tr: The output format is HH:MM:SS PM.

strpos

This function returns the position of the first occurrence of the target substring in a string. It returns 0 if the target substring is not found.

Syntax: strpos(str, subStr)

Table 30 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

subStr

Target substring.

String

Yes

Return value type: integer

Example: SELECT STRPOS('hello world', 'llo'), STRPOS('llo', 'hello world')

Table 31 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

STRPOS('hello world', 'llo')

3

Scenario 2

STRPOS('llo', 'hello world')

0

substr

This function extracts a substring based on the start position and length. If the length is not specified, it returns the entire string starting from the start position. It is equivalent to the substring function.

Syntax: substr(str, start, length)

Table 32 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

start

Start position of the target substring to be returned.

Integer

Yes

length

Length of the substring.

Integer

No

Return value type: string

Example: SELECT SUBSTR('helloworld', 5), SUBSTR('helloworld', 5, 3)

Table 33 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

SUBSTR ('helloworld', 5)

oworld

Scenario 2

SUBSTR ('helloworld', 5, 3)

owo

substring

This function extracts a substring based on the start position and length. If the length is not specified, it returns the entire string starting from the start position.

Syntax: substring(str, start, length)

Table 34 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

start

Start position for extracting the substring. Valid values start from 1.

Integer

Yes

length

Length of the substring.

Integer

No

Return value type: string

Example: SELECT SUBSTRING('helloworld', 5), SUBSTRING('helloworld', 5, 3)

Table 35 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

SUBSTRING('helloworld', 5)

oworld

Scenario 2

SUBSTRING('helloworld', 5, 3)

owo

length

This function returns the length of the input characters (for a string expression) or the byte length (for a non-string expression). The byte length is at least the smallest integer obtained by dividing the number of digits by 8.

Syntax: length(str)

Table 36 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

Return value type: integer

Example: SELECT LENGTH('HELLO WORLD')

Table 37 Query and analysis results

Type

Query Statement

Returned Result

Scenario

LENGTH('HELLO WORLD')

11

char_length

This function returns the length of the input characters (for a string expression) or the byte length (for a non-string expression). The byte length is at least the smallest integer obtained by dividing the number of digits by 8. This function is equivalent to the length function.

Syntax: char_length(str)

Table 38 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

Return value type: integer

Example: SELECT CHAR_LENGTH('HELLO WORLD')

Table 39 Query and analysis results

Type

Query Statement

Returned Result

Scenario

CHAR_LENGTH('HELLO WORLD')

11

character_length

This function is equivalent to the length function.

Syntax: character_length(str)

Table 40 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

Return value type: integer

Example: SELECT CHARACTER_LENGTH('HELLO WORLD')

Table 41 Query and analysis results

Type

Query Statement

Returned Result

Scenario

CHARACTER_LENGTH('HELLO WORLD')

11

strlen

This function is equivalent to the length function.

Syntax: strlen(str)

Table 42 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

Return value type: integer

Example: SELECT STRLEN('HELLO WORLD')

Table 43 Query and analysis results

Type

Query Statement

Returned Result

Scenario

STRLEN('HELLO WORLD')

11

levenshtein_distance

This function calculates the minimum edit distance between strings str1 and str2.

Syntax: levenshtein_distance(str1, str2)

Table 44 Parameter description

Parameter

Description

Type

Mandatory

str1

Original string 1.

String

Yes

str2

Original string 2.

String

Yes

Return value type: integer

Example: SELECT LEVENSHTEIN_DISTANCE('horse', 'ros')

Table 45 Query and analysis results

Type

Query Statement

Returned Result

Scenario

LEVENSHTEIN_DISTANCE('horse', 'ros')

3

normalize

This function formats a string (str).

  • Formats str using the NFC form.

    Syntax: normalize(str)

  • Formats str using a specified form. The supported forms include NFC, NFD, NFKC, and NFKD.

    Syntax: normalize(str, form)

Table 46 Parameter description

Parameter

Description

Type

Mandatory

str

Original string.

String

Yes

form

Formatting form.

String

No

Return value type: string

Example: SELECT NORMALIZE('schön'), NORMALIZE('Henry \u2163', 'nfd')

Table 47 Query and analysis results

Type

Query Statement

Returned Result

Scenario 1

NORMALIZE('schön')

schön

Scenario 2

NORMALIZE('Henry \u2163', 'nfd')

Henry IV

to_utf8

This function converts a string to the UTF-8 encoding format.

Syntax: to_utf8(str)

Table 48 Parameter description

Parameter

Description

Type

Mandatory

str1

Original string 1.

String

Yes

str2

Original string 2.

String

Yes

Return value type: integer

Example: SELECT TO_UTF8('%')

Table 49 Query and analysis results

Type

Query Statement

Returned Result

Scenario

TO_UTF8('%')

JQ==

chr

This function converts an ASCII value to characters.

Syntax: chr(ascii)

Table 50 Parameter description

Parameter

Description

Type

Mandatory

ascii

ASCII code.

Integer

Yes

Return value type: string

Example: SELECT CHR(99)

Table 51 Query and analysis results

Type

Query Statement

Returned Result

Scenario

CHR(99)

c

concat

This function concatenates multiple parameters into a string.

Syntax: concat(x, y...)

Table 52 Parameter description

Parameter

Description

Type

Mandatory

x,y...

Original field.

String

Yes

Return value type: string

Example: select concat('1','+','1')

Table 53 Query and analysis results

Type

Query Statement

Returned Result

Scenario

concat('1','+','1')

1+1