String functions

Updated at:

The following table lists the string functions.

Function

Features

CONCAT

Concatenates multiple strings.

CONCAT_WS

Concatenates multiple strings with a specified separator.

FIND_IN_SET

Finds the position of a specified string within a comma-separated string.

FORMAT_NUMBER

Formats a number into a string with a thousands separator and a specified number of decimal places.

JSON_EXTRACT

Extracts a value from a JSON string at a specified path.

LENGTH

Calculates the length of a string.

LOWER

Converts uppercase characters in a string to lowercase.

LPAD

Pads a string on the left to a specified length.

LTRIM

Removes leading whitespace from a string.

REMOVE_STRING

Removes specified characters from a string.

REPLACE

Replaces all occurrences of a specified substring with another string.

RPAD

Pads a string on the right to a specified length.

RTRIM

Removes trailing whitespace from a string.

SUB_STRING

Extracts a substring from a string at a specified position.

TRIM

Removes leading and trailing whitespace from a string.

UPPER

Converts lowercase characters in a string to uppercase.

CONCAT

  • Function description

Concatenates multiple strings.

  • Parameters

    • expr1: Required. A value of any data type. This is the first parameter to concatenate.

    • expr2: Required. A value of any data type. This is the second parameter to concatenate.

    • exprN: Optional. A value of any data type. This is the Nth parameter to concatenate.

  • Return value

Returns the concatenated string. If any input parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

CONCAT_WS

  • Function description

Concatenates multiple strings with a specified separator.

  • Parameters

    • separator: Required. A value of the STRING type. This parameter specifies the separator.

    • expr1: Required. A value of any data type. This is the first parameter to concatenate.

    • expr2: Required. A value of any data type. This is the second parameter to concatenate.

    • exprN: Optional. A value of any data type. This is the Nth parameter to concatenate.

  • Return value

Returns the concatenated string. If any input parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

FIND_IN_SET

  • Function description

This function finds a substring within a comma-separated string. It returns the 1-based position of subStr in the comma-separated string listStr. If the substring is not found, the function returns 0. If subStr or listStr is NULL, the function returns NULL.

  • Parameters

    • subStr: Required. A value of the STRING type. This parameter specifies the substring to find.

    • listStr: Required. A value of the STRING type. This parameter specifies the comma-separated string.

  • Return value

Returns the 1-based position of subStr in listStr. If the substring is not found, this function returns 0. If subStr or listStr is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, and Spark.

FORMAT_NUMBER

  • Function description

Formats a number into a string with a thousands separator and a specified number of decimal places.

  • Parameters

    • number: Required. A value of a numeric type. This parameter specifies the number to format.

    • scale: Required. A value of the INT type. This parameter specifies the number of decimal places to retain.

  • Return value

Returns the formatted string. If number is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

JSON_EXTRACT

  • Function description

Extracts a string value from a JSON string source at a specified path.

The path uses the following format:

'$' represents the root node.

'.' represents a child node, such as $.a.

[number] represents an array element. The index is 0-based, such as $.a[2].

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source JSON data.

    • path: Required. A value of the STRING type. This parameter specifies the path from which to extract data.

  • Return value

Returns the corresponding string if the JSON is valid and the path exists. This function returns NULL if the source is not a valid JSON string, the path is NULL, or the content at the specified path does not exist.

  • Supported compute engines

MaxCompute (ODPS), MySQL 5.7 or later, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

LENGTH

  • Function description

This function calculates the length of the string str. If str is NULL, this function returns NULL.

  • Parameters

    • str: Required. A value of the STRING type. This parameter specifies the string whose length you want to calculate.

  • Return value

Returns the length of the string str. If str is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

LOWER

  • Function description

Converts uppercase characters in the string source to their corresponding lowercase characters.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

  • Return value

Returns the string converted to lowercase. If source is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

LPAD

  • Function description

This function pads the left side of the string str1 with the string str2 to a total length of length characters.

  • Parameters

    • str1: Required. A value of the STRING type. This parameter specifies the string to pad.

    • length: Required. A value of the INT type. This parameter specifies the target length after padding.

    • str2: Required. A value of the STRING type. This parameter specifies the string to use for padding.

  • Return value

Returns the left-padded string. If any input parameter is NULL, this function returns NULL. If the value of length is less than the length of str1, this function returns str1 truncated to length characters from the left.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

LTRIM

  • Function description

Removes leading whitespace from the string source.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

  • Return value

Returns the string source with leading whitespace removed. If source is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

REMOVE_STRING

  • Function description

This function removes all occurrences of the subStr string from the source string and returns the new string. If no matching strings are found, this function returns the original source string.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

    • subStr: Required. A value of the STRING type. This parameter specifies the substring to remove.

  • Return value

Returns the new string after the substring is removed. If any input parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

REPLACE

  • Function description

This function replaces all occurrences of the oldStr string in the source string with the newStr string and returns the new string. If no matching strings are found, this function returns the original source string.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

    • oldStr: Specifies the string to be compared. This is a required string parameter.

    • newStr: Required. A value of the STRING type. This parameter specifies the replacement string.

  • Return value

Returns the new string after replacement. If any input parameter is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

RPAD

  • Function description

This function pads the right side of the string str1 with the string str2 to a total length of length characters.

  • Parameters

    • str1: Required. A value of the STRING type. This parameter specifies the string to pad.

    • length: Required. A value of the INT type. This parameter specifies the target length after padding.

    • str2: Required. A value of the STRING type. This parameter specifies the string to use for padding.

  • Return value

Returns the right-padded string. If any input parameter is NULL, this function returns NULL. If the value of length is less than the length of str1, this function returns str1 truncated to length characters from the left.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

RTRIM

  • Function description

Removes trailing whitespace from the string source.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

  • Return value

Returns the string source with trailing whitespace removed. If source is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

SUB_STRING

  • Function description

This function returns a substring of the specified length from the source string, starting at the specified startPosition.

The counting rules for startPosition are as follows:

startPosition is 1-based.

If startPosition is 0, this function returns an empty string.

If startPosition is a negative number, the starting position is counted backward from the end of the string. The last character is -1.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

    • startPosition: Required. A value of the INT type. This parameter specifies the starting position of the substring.

    • length: Optional. A value of the INT type. This parameter specifies the length of the substring. The value must be greater than 0. If this parameter is set to the constant value null, the function returns the substring from the start position to the end of source.

  • Return value

Returns the extracted substring. If source or startPosition is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

TRIM

  • Function description

Removes leading and trailing whitespace from the string source.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

  • Return value

Returns the string source with leading and trailing whitespace removed. If source is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.

UPPER

  • Function description

This function converts lowercase characters in the source string to their corresponding uppercase characters. If source is NULL, this function returns NULL.

  • Parameters

    • source: Required. A value of the STRING type. This parameter specifies the source string.

  • Return value

Returns the string converted to uppercase. If source is NULL, this function returns NULL.

  • Supported compute engines

MaxCompute (ODPS), MySQL, RDS, Hive, AnalyticDB for PostgreSQL (ADB_PG), PolarDB for PostgreSQL (POLAR_PG), PostgreSQL, and Spark.