Type Conversion Functions
During the routine use of data warehouses, users often need to convert data between different types. For example, when importing data from an external data source, the date may be stored as a string. When analyzing data, you need to format the value into a string with a specific precision for report display. When processing log data, you need to restore the hexadecimal code to the original binary content. DWS provides various type conversion functions, covering scenarios such as date and time conversion, value formatting, character set conversion, and conversion between binary and hexadecimal.
However, the syntax, applicable versions, and return value types of different functions are different. You need to select a proper function and correctly specify the format template. Otherwise, the conversion may fail or the result may not meet the expectation. This section describes the syntax, parameters, return values, and examples of each type of conversion functions in DWS, helping you correctly perform type conversion in actual services.
Classification of Type Conversion Functions
DWS type conversion functions can be classified into the following eight types based on the conversion direction and usage:
| Category | Function | Description |
|---|---|---|
| cast(x as y), cast(x, y), try_cast(x as type) | General conversion between any types, without requiring a format template. | |
| Numeric Formatting Functions (Converting Numbers to Strings) | to_char(double precision, text), to_char(integer/number [, fmt]), to_char(int, text), to_char(numeric, text) | Converts a number to a string based on the format template. |
| Date and Time Formatting Functions (Converting Date and Time to Strings) | to_char(datetime/interval [, fmt]), to_char(interval, text), to_char(timestamp, text) | Converts date and time to a string based on the format template. |
| to_char(string), to_clob(...) | Conversion between string types. | |
| to_number(expr [, fmt]), to_number(text, text) | Converts a string to a value in the specified format. | |
| to_date(text), to_date(text, text), to_date(string, fmt), to_timestamp(double precision), to_timestamp(string [,fmt]), to_timestamp(text, text) | Converts a string or UNIX timestamp to a date or timestamp. | |
| hextoraw(string), rawtohex(string) | Converts between hexadecimal strings and binary strings. | |
| numtoday(numeric), pg_systimestamp() | Converts a value to a time or obtains the system timestamp. |
General-Purpose Type Conversion Functions
General-purpose type conversion functions are used to convert between any data types, and no format template needs to be specified.
cast(x as y)
Description: Converts x into the type specified by y.
Example:
1 2 3 4 5 | SELECT cast('22-oct-1997' as timestamp); timestamp --------------------- 1997-10-22 00:00:00 (1 row) |
cast(x, y)
Description: Converts x into the type specified by y. This function is supported by version 8.2.0 or later clusters.
Example:
1 2 3 4 5 | SELECT cast('22-oct-1997', timestamp); timestamp --------------------- 1997-10-22 00:00:00 (1 row) |
try_cast(x as type)
Description: Converts x to a value of the given type. If the conversion fails and the current type is converted to a value allowed by DWS, NULL is returned. Otherwise, an error is raised. This function is supported by version 8.2.0 or later clusters.
Example:
1 2 3 4 5 6 7 8 9 10 | SELECT try_cast('a' as int4); int4 --------------- (1 row) SELECT try_cast('22-oct-1997' as timestamp); timestamp --------------------- 1997-10-22 00:00:00 (1 row) |
Numeric Formatting Functions (Converting Numbers to Strings)
The following functions convert numeric types (such as integers, floating-point numbers, and double precision) to strings based on the specified format template.
to_char(double precision, text)
Description: Converts the values of the double precision type into the strings in the specified format.
Return type: text.
Example:
1 2 3 4 5 | SELECT to_char(125.8::real, '999D99'); to_char --------- 125.80 (1 row) |
to_char (integer/number[, fmt])
Description: Converts an integer or a floating-point value into a string in the specified format.
- The optional parameter fmt can be one of the following types: decimal characters, grouping separators, signs, and currency symbols. Each type can have different templates. Different templates can be combined properly. Common templates include 9, 0, , (thousands separator), and . (decimal point). For details, see the table of numeric format template patterns.
- A template can have a modifier, similar to FM. However, FM does not suppress the 0 that is output as specified by the template 0.
- To convert an integer value into a string in hexadecimal format, use the template x or X.
Return type: varchar.
Example:
1 2 3 4 5 | SELECT to_char(1485,'9,999'); to_char --------- 1,485 (1 row) |
1 2 3 4 5 | SELECT to_char( 1148.5,'9,999.999'); to_char ------------ 1,148.500 (1 row) |
1 2 3 4 5 | SELECT to_char(148.5,'990999.909'); to_char ------------- 0148.500 (1 row) |
1 2 3 4 5 | SELECT to_char(123,'XXX'); to_char --------- 7B (1 row) |
to_char(int, text)
Description: Converts a value of the integer type into a string in the specified format.
Return type: text.
Example:
1 2 3 4 5 | SELECT to_char(125, '999'); to_char --------- 125 (1 row) |
to_char(numeric, text)
Description: Converts the values of the numeric type into the strings in the specified format.
Return type: text.
Example:
1 2 3 4 5 | SELECT to_char(-125.8, '999D99S'); to_char --------- 125.80- (1 row) |
Date and Time Formatting Functions (Converting Date and Time to Strings)
The following functions convert the date and time types (such as DATE, TIMESTAMP, and INTERVAL) to strings in the specified format.
to_char (datetime/interval [, fmt])
Description: Converts a DATETIME or INTERVAL value of the DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIMESTAMP WITH LOCAL TIME ZONE type to the VARCHAR type according to the format specified by fmt.
- The optional parameter fmt can be one of the following types: date, time, day of week, quarter, and century. Each type can have different templates. Different templates can be combined properly. Common templates include HH, MI, SS, YYYY, MM, and DD. For details, see Table 3.
- A template can have a modifier. The common modifier is FM, which can be used to suppress the leading zero or the trailing blank.
Return type: varchar.
Example:
1 2 3 4 5 | SELECT to_char(current_timestamp,'HH12:MI:SS'); to_char ---------- 10:19:26 (1 row) |
1 2 3 4 5 | SELECT to_char(current_timestamp,'FMHH12:FMMI:FMSS'); to_char ---------- 10:19:46 (1 row) |
to_char(interval, text)
Description: Converts the values of the time interval type into the strings in the specified format.
Return type: text.
Example:
1 2 3 4 5 | SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS'); to_char ---------- 15:02:12 (1 row) |
to_char(timestamp, text)
Description: Converts the values of the timestamp type into the strings in the specified format.
Return type: text.
Example:
1 2 3 4 5 | SELECT to_char(current_timestamp, 'HH12:MI:SS'); to_char ---------- 10:55:59 (1 row) |
String Conversion Functions
The following functions are used to convert between different string types (CHAR, VARCHAR, and CLOB).
to_char (string)
Description: Converts the CHAR, VARCHAR, VARCHAR2, or CLOB type into the VARCHAR type.
If this function is used to convert data of the CLOB type, and the value to be converted exceeds the value range of the target type, an error is returned.
Return type: varchar.
Example:
1 2 3 4 5 | SELECT to_char('01110'); to_char --------- 01110 (1 row) |
to_clob(char/nchar/varchar/nvarchar/varchar2/nvarchar2/text/raw)
Description: Converts the RAW type or text character set types CHAR, NCHAR, VARCHAR, VARCHAR2, NVARCHAR2, and TEXT into the CLOB type.
Return type: clob.
Example:
1 2 3 4 5 | SELECT to_clob('ABCDEF'::RAW(10)); to_clob --------- ABCDEF (1 row) |
1 2 3 4 5 | SELECT to_clob('hello111'::CHAR(15)); to_clob ---------- hello111 (1 row) |
1 2 3 4 5 | SELECT to_clob('gauss123'::NCHAR(10)); to_clob ---------- gauss123 (1 row) |
1 2 3 4 5 | SELECT to_clob('gauss234'::VARCHAR(10)); to_clob ---------- gauss234 (1 row) |
1 2 3 4 5 | SELECT to_clob('gauss345'::VARCHAR2(10)); to_clob ---------- gauss345 (1 row) |
1 2 3 4 5 | SELECT to_clob('gauss456'::NVARCHAR2(10)); to_clob ---------- gauss456 (1 row) |
1 2 3 4 5 | SELECT to_clob('World222!'::TEXT); to_clob ----------- World222! (1 row) |
String-to-Number Conversion Functions
The following functions convert a string into a numeric type based on the specified format template.
to_number ( expr [, fmt])
Description: Converts expr into a value of the NUMBER type according to the specified format.
For details about the conversion formats, see Table 2.
If a hexadecimal string is converted into a decimal number, the hexadecimal string can include a maximum of 16 bytes if it is to be converted into an unsigned number.
During the conversion from a hexadecimal string to a decimal number, the format string cannot contain a character other than x or X. Otherwise, an error is reported.
Return type: number.
Example:
1 2 3 4 5 | SELECT to_number('12,454.8-', '99G999D9S'); to_number ----------- -12454.8 (1 row) |
to_number(text, text)
Description: Converts a value of the string type into a number in the specified format.
Return type: numeric.
Example:
1 2 3 4 5 | SELECT to_number('12,454.8-', '99G999D9S'); to_number ----------- -12454.8 (1 row) |
String-to-Date/Time Functions
The following functions convert a string or a UNIX timestamp to the date or timestamp type.
to_date(text)
Description: Converts values of the text type into the timestamp in the specified format.
Return type: timestamp.
Example:
1 2 3 4 5 | SELECT to_date('2015-08-14'); to_date --------------------- 2015-08-14 00:00:00 (1 row) |
to_date(text, text)
Description: Converts values of the string type into the dates in the specified format.
Return type: timestamp.
Example:
1 2 3 4 5 | SELECT to_date('05 Dec 2000', 'DD Mon YYYY'); to_date --------------------- 2000-12-05 00:00:00 (1 row) |
to_date(string, fmt)
Description: Converts the string into a value of the DATE type in the format specified by fmt. For details about the fmt format, see Table 3.
This function does not directly support the CLOB type, but parameters of the CLOB type can be converted implicitly.
Return type: date.
Example:
1 2 3 4 5 | SELECT TO_DATE('05 Dec 2010','DD Mon YYYY'); to_date --------------------- 2010-12-05 00:00:00 (1 row) |
to_timestamp(double precision)
Description: Converts a UNIX century into a timestamp.
Return type: timestamp with time zone.
Example:
1 2 3 4 5 | SELECT to_timestamp(1284352323); to_timestamp ------------------------ 2010-09-13 12:32:03+08 (1 row) |
to_timestamp(string [,fmt])
Description: Converts the string into a value of the timestamp type in the format specified by fmt. If fmt is not specified, the format specified by the nls_timestamp_format parameter is used. For details about the fmt format, see Table 3.
In the to_timestamp function of DWS:
- If the input year YYYY is 0, an error will be reported.
- If the input year is YYYY<0 and SYYYY is specified in fmt, the year with the absolute value of n is correctly output.
The characters in the fmt string must match the date/time formatting pattern. Otherwise, an error is reported.
Return type: timestamp without time zone.
Example:
1 2 3 4 5 6 7 8 9 10 11 | SHOW nls_timestamp_format; nls_timestamp_format ---------------------------- DD-Mon-YYYY HH:MI:SS.FF AM (1 row) SELECT to_timestamp('12-sep-2014'); to_timestamp --------------------- 2014-09-12 00:00:00 (1 row) |
1 2 3 4 5 | SELECT to_timestamp('12-Sep-10 14:10:10.123000','DD-Mon-YY HH24:MI:SS.FF'); to_timestamp ------------------------- 2010-09-12 14:10:10.123 (1 row) |
1 2 3 4 5 | SELECT to_timestamp('-1','SYYYY'); to_timestamp ------------------------ 0001-01-01 00:00:00 BC (1 row) |
1 2 3 4 5 | SELECT to_timestamp('98','RR'); to_timestamp --------------------- 1998-01-01 00:00:00 (1 row) |
1 2 3 4 5 | SELECT to_timestamp('01','RR'); to_timestamp --------------------- 2001-01-01 00:00:00 (1 row) |
to_timestamp(text, text)
Description: Converts a value of the string type into a timestamp of the specified format.
Return type: timestamp.
Example:
1 2 3 4 5 | SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY'); to_timestamp --------------------- 2000-12-05 00:00:00 (1 row) |
Binary and Hexadecimal Conversion Functions
The following functions are used to convert between hexadecimal strings and binary types.
hextoraw(string)
Description: Converts a string in hexadecimal format into binary format.
Return type: raw
Example:
1 2 3 4 5 | SELECT hextoraw('7D'); hextoraw ---------- 7D (1 row) |
rawtohex(string)
Description: Converts a string in binary format into hexadecimal format.
The result is the ASCII code of the input characters in hexadecimal format.
Return type: varchar.
Example:
1 2 3 4 5 | SELECT rawtohex('1234567'); rawtohex ---------------- 31323334353637 (1 row) |
numtoday(numeric)
Description: Converts values of the number type into the timestamp of the specified type.
Return type: timestamp.
Example:
1 2 3 4 5 | SELECT numtoday(2); numtoday ---------- 2 days (1 row) |
pg_systimestamp()
Description: Obtains the system timestamp.
Return type: timestamp with time zone
Example:
1 2 3 4 5 | SELECT pg_systimestamp(); pg_systimestamp ------------------------------- 2015-10-14 11:21:28.317367+08 (1 row) |
Format Template Reference
Table 2 describes the template patterns that can be used to format numbers, which are applicable to the to_number function.
| Schema | Description |
|---|---|
| 9 | Value with specified digits |
| 0 | Values with leading zeros |
| Period (.) | Decimal point |
| Comma (,) | Group (thousand) separator |
| PR | Negative values in angle brackets |
| S | Sign anchored to number (uses locale) |
| L | Currency symbol (uses locale) |
| D | Decimal point (uses locale) |
| G | Group separator (uses locale) |
| MI | Minus sign in the specified position (if the number is less than 0) |
| PL | Plus sign in the specified position (if the number is greater than 0) |
| SG | Plus or minus sign in the specified position |
| RN | Roman numerals (the input values are between 1 and 3999) |
| TH or th | Ordinal number suffix |
| V | Shifts specified number of digits (decimal) |
Table 3 describes the patterns of date and time values. They can be used for functions to_date, to_timestamp, and to_char, and the nls_timestamp_format parameter.