Data type conversion functions
This topic describes the data type conversion functions supported by Hologres. This topic also provides examples on how to use the functions.
Function | Return value type | Description | Example | Returned result | Remarks |
TRY_CAST |
| Converts data of the TEXT type into another data type. If the data cannot be converted into the desired data type, this function does not return an error message but returns NULL. |
| The data abc in the value column cannot be converted into the INT type. The | This function is an additional function of Hologres. The TRY_CAST function can be used only in Hologres V2.2. If the version of your Hologres instance is V2.1 or earlier, contact the Hologres technical support to upgrade your Hologres instance. |
to_char(timestamp, text) | TEXT | Converts a timestamp into a string. By default, only timestamps that describe time points in a year from 1925 to 2282 can be converted. | to_char(current_timestamp, 'HH12:MI:SS') | 06:26:33 | In Hologres V1.1.31 and later, you can execute the Note If you configure the GUC parameter, the query performance degrades by about 50%. In Hologres V1.1.42 and later, the query performance degrades by about 20%. |
to_char(int, text) | TEXT | Converts an integer into a string. | to_char(125, '999') | 125 | None. |
to_char(double precision, text) | TEXT | Converts a real number or a double-precision number into a string. | to_char(125.8::real, '999D9') | 125.8 | None. |
to_date(text, text) | DATE | Converts a string into a date. By default, only strings that describe time points in a year from 1925 to 2282 can be converted. |
| 2000-12-05 | In Hologres V1.1.31 and later, you can execute the Note If you configure the GUC parameter, the query performance degrades by about 50%. In Hologres V1.1.42 and later, the query performance degrades by about 20%. |
to_number(text, text) | NUMERIC | Converts a string into a number. | to_number('12,454.8-', '99G999D9S') | -12454.8 | None. |
to_timestamp(text, text) | TIMESTAMP | Converts a string into a timestamp. By default, only strings that describe time points in a year from 1925 to 2282 can be converted. | to_timestamp('05 Dec 2000', 'DD Mon YYYY') | 2000-12-05 00:00:00 |
Note If you configure the GUC parameter, the query performance degrades by about 50%. In Hologres V1.1.42 and later, the query performance degrades by about 20%. |
array_to_string(anyarray, text [, text]) | TEXT | Converts an array into a string. | array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*') | 1,2,3,*,5 | None. |
array_agg(expression) | ARRAY | Aggregates input values into an array. This function can be used to convert a string into an array or convert column-oriented data into row-oriented data. |
|
| None. |
array_agg (expression[ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ] | ARRAY | Aggregates input values into an array. This function can be used to convert a string into an array or convert column-oriented data into row-oriented data. You can use a FILTER clause in the function expression. | array_agg(name) filter (where id >3) from f_test | {a5,a6,a7,a7,"{namea,nameb,namec}",a9,a4} | You can use a FILTER clause in the function expression only if the version of your Hologres instance is V1.3 or later. |
string_agg(expression) | TEXT | Aggregates non-null input values into a string, separated by a specified delimiter. This function can be used to convert column-oriented data into row-oriented data. | string_agg(colname, '-') | a-b-c | None. |
string_agg (expression [ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ] | TEXT | Aggregates non-null input values into a string, separated by a specified delimiter. This function can be used to convert column-oriented data into row-oriented data. You can use a FILTER clause in the function expression. | string_agg(name,',') filter (where id >3) from f_test | a7,a7,a5,a4,{namea,nameb,namec},a9,a6 | You can use a FILTER clause in the function expression only if the version of your Hologres instance is V1.3 or later. |
regexp_split_to_table(string text, pattern text ) | TEXT | Splits a string by using a Portable Operating System Interface (POSIX) regular expression. This function can be used to convert row-oriented data into column-oriented data. | regexp_split_to_table('hello world', '\s+') | hello world | None. |
isnumeric(text) | BOOLEAN | Checks whether all characters in a string are numeric characters. | isnumeric('95.5') | true | This function is supported in Hologres V1.1 and later. |
row_to_json | JSON | Converts data in a row into the JSON format. | row_to_json(t) from (select col1,col2 ,...from table) as t | "{"f1":"1","f2":"0"}""{"f1":"0","f2":"0"}""{"f1":"fa","f2":"fb"}""{"f1":"fa","f2":"fb"}" | This function is supported in Hologres V1.3 and later. |
row() | ARRAY or TEXT | Returns the number of the row in which a formula is located. | select a,b from row_table where row(a,b) =row(b,a) ; | | This function is supported in Hologres V1.3 and later. |