If you need to convert or intercept strings in Hologres, you can use standard PostgreSQL string functions that are compatible with Hologres for data development. This topic describes the string conversion functions and string interception functions that are supported by Hologres and provides sample statements of these functions.
String conversion functions
The following table describes the string conversion functions supported by Hologres. The functions supported by Hologres are only a subset of the PostgreSQL functions. For more information about how to use these functions, see Functions and Operators.
Function | Return value type | Description | Example | Returned result |
to_number(text, text) | NUMERIC | Converts a string into a number. |
| -12454.8 |
string_agg(expression,delimiter) | TEXT | Concatenates the non-null values of the specified expression into a string by using a delimiter. This function can be used to transpose columns to rows. |
|
|
string_agg (expression [ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ] | TEXT | Concatenates the non-null values of an expression into a string by using a delimiter. This function can be used to transpose columns to rows. You can use a FILTER clause in the function expression. Note Hologres V1.3 and later support this function. |
|
|
regexp_split_to_table(string text, pattern text) | TEXT | A string splitting function. This function transposes the split data to rows and can be used to transpose rows to columns. |
|
|
regexp_split_to_array(string text, pattern text) | ARRAY | A string splitting function. This function transposes the split data to arrays and can be used to transpose rows to arrays. |
|
|
replace(string text, old_text, new_text) | TEXT | Replaces a substring in a string with another substring. |
|
|
regexp_replace(string, regex, replacement[, flags]) | TEXT | Replaces a substring with another substring by using a Portable Operating System Interface (POSIX) regular expression. |
|
|
regexp_match(string text, pattern text) | ARRAY | Matches the content of a string by using regular expressions. Content fragments that meet the matching conditions are displayed in an array. |
|
|
row_to_json | JSON | Converts a row of data into a JSON object. Note
|
|
|
row() | ARRAY/TEXT | Returns the number of the row in which a formula is located. Note Hologres V1.3 and later support this function. |
|
|
String interception functions
The following table describes the string interception functions supported by Hologres. The functions supported by Hologres are only a subset of the PostgreSQL functions. For more information about how to use these functions, see String Functions and Operators in the PostgreSQL documentation.
Function | Return value type | Description | Example | Returned result |
split_part(string text ,delimiter text,n int) | TEXT | Splits a string with a specific delimiter and returns the Nth string. |
|
|
concat(str "any" [, str "any" [, ...] ]) | TEXT | Concatenates all strings. This function ignores strings with NULL values. |
|
|
concat_ws(sep text, str "any" [, str "any" [, ...] ]) | TEXT | Concatenates strings excluding the first string with a delimiter. Note The first string is used as the delimiter and is not concatenated. This function ignores strings with NULL values. |
|
|
substring(string [from int] [for int]) | TEXT | Returns a substring extracted from a string based on the specified character location for a specific number of characters. |
|
|
substring(string from pattern) | TEXT | Returns a substring that matches a Portable Operating System Interface (POSIX) regular expression in a string. |
|
|
substring(string from pattern for escape) | TEXT | Returns a substring that matches an SQL regular expression in a string. |
|
|
left(string text, n int) | TEXT | Returns the first n characters of a string. If n is set to a negative value, characters excluding the last |
|
|
right(string text, n int) | TEXT | Returns the last n characters of a string. If n is set to a negative value, characters excluding the first |
|
|
string || string | TEXT | Concatenates two strings. |
| HologreSQL |
bit_length(string) | TEXT | Returns the length of a string as the number of bits. |
| 32 |
char_length(string) | TEXT | Returns the length of a string as the number of characters. |
| 4 |
length(string) | TEXT | Returns the length of a string as the number of bytes. When the UTF-8 character set is used to encode the string, one Chinese character occupies three bytes, and one digit or letter occupies one byte. Note For a string that contains only letters, the length(string) function returns the same result as the char_length(string) function because one letter occupies one byte. |
| 4 |
lower(string) | TEXT | Converts a string into lowercase letters. |
| tom |
upper(string) | TEXT | Converts a string into uppercase letters. |
| TOM |
initcap(string) | TEXT | Converts the first letter of each word in a string to an uppercase letter and converts the rest to lowercase letters. Note A word consists of letters and digits. Words are separated with characters other than letters or digits. |
| Hi Thomas |
octet_length(string) | TEXT | Returns the length of a string as the number of octets. |
| 4 |
position(substring in string) | TEXT | Returns the location of a substring in a string. |
| 3 |
strpos(string, substring) | TEXT | Returns the location of a substring in a string. |
| 2 |
trim([leading | trailing | both] [characters] from string) | TEXT | Removes the longest string that contains specific characters from the start, end, or both sides of a string. Note
|
| Tom |
btrim(string text [, characters text]) | TEXT | Removes the longest string that contains specific characters from the start and the end of a string. Note If you do not specify the trimming characters, this function removes spaces from specific parts of a string. |
| trim |
ltrim(string text [, characters text]) | TEXT | Removes the longest string that contains specific characters from the start of a string. Note If you do not specify the trimming characters, this function removes spaces from the start of a string. |
| test |
rtrim(string text [, characters text]) | TEXT | Removes the longest string that contains specific characters from the end of a string. Note If you do not specify the trimming characters, this function removes spaces from the end of a string. |
| test |
lpad(string text, length int [, fill text]) | TEXT | Prepends characters to the start of a string based on a specific length. Note
|
| xyxhi |
rpad(string text, length int [, fill text]) | TEXT | Prepends characters to the right side of a string based on a specific length. |
| hixyx |
md5(string) | TEXT | Calculates the MD5 hash of a string. The MD5 hash is a 32-character hexadecimal number. |
| 900150983cd24fb0d6963f7d28e17f72 |
parse_ident(quali_iden text [,...] ) | TEXT | Parses a string. |
| {SomeSchema,sometable} |
quote_ident(string text) | TEXT | Returns a string as a double-quoted string to be used as an identifier in an SQL statement. Note This function adds double quotation marks (") when the string contains non-identifier characters or would be case folded. |
| "Foo bar" |
quote_literal(string text) | TEXT | Returns a string as a quoted string to be used as a string literal in an SQL statement. |
| 'O''Reilly' |
ascii(string) | TEXT | Returns the ASCII value of the first character in a string. |
| 120 |
chr(int) | TEXT | Returns the character that matches an encoded value. Note The value must be valid and encoded in ASCII or UTF-8. In addition, the value cannot be 0. |
| A |
repeat(string text, number int) | TEXT | Repeats a string for a specific number of times. |
| PgPgPgPg |
starts_with(string, prefix) | BOOLEAN | If the string starts with the specified prefix, the value |
| t |
to_hex(number int or bigint) | TEXT | Converts a number to its equivalent hexadecimal value. |
| 7fffffff |
translate(string text, from text, to text) | TEXT | Replaces all occurrences of the characters in the from set with the characters in the to set. |
| a2x5 |