Type Conversion Functions
Type Conversion Functions
- cash_words(money)
Description: Type conversion function, which converts money into text.
Example:
1 2 3 4 5
gaussdb=# SELECT cash_words('1.23'); cash_words ----------------------------------- One dollar and twenty three cents (1 row)
- convert(expr, type)
Description: Converts expr to the type specified by type.
Parameter: The first parameter is an arbitrary value, and the second parameter is the type name.
Return value type: The return value type is the same as that of the input.
Example:
1 2 3 4 5
gaussdb=# SELECT convert(12.5, text); text ------ 12.5 (1 row)
This function takes effect only in databases in MySQL compatibility mode.
- cast(x as y [DEFAULT z ON CONVERSION ERROR][,fmt])
Description: Converts x into the type specified by y. When sql_compatibility is set to 'MYSQL', b_format_version is set to '5.7', and b_format_dev_version is set to 's1', if y is of the char type, this function converts x to the varchar type.
- DEFAULT z ON CONVERSION ERROR: This parameter is optional. If the attempt to convert x to type y fails, z is converted to type y by default.
- fmt: This parameter is optional, which can be specified when y is one of the following data types:
- int1/int2/int4/int8/int16/float4/float8/numeric: The function of the optional parameter fmt is the same as that in the to_number(expr [,fmt]) function.
- date/timestamp/timestamp with time zone: The function of the optional parameter fmt is the same as that in the to_date(string [,fmt])/to_timestamp(string [,fmt]) /to_timestamp_tz(string [,fmt]) function.
Example:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
gaussdb=# SELECT cast('22-oct-1997' as timestamp); timestamp --------------------- 1997-10-22 00:00:00 (1 row) gaussdb=# SELECT cast('22-ocX-1997' as timestamp DEFAULT '22-oct-1997' ON CONVERSION ERROR, 'DD-Mon-YYYY'); timestamp --------------------- 1997-10-22 00:00:00 (1 row) gaussdb=# CREATE DATABASE gaussdb_m WITH dbcompatibility 'MYSQL'; gaussdb=# \c gaussdb_m -- Set compatible version control parameters. gaussdb_m=# SET b_format_version='5.7'; gaussdb_m=# SET b_format_dev_version='s1'; gaussdb_m=# SELECT cast('aaa' as char); varchar --------- aaa (1 row)
The DEFAULT z ON CONVERSION ERROR and fmt syntaxes are supported only when a_format_version is set to 10c and a_format_dev_version is set to s1.
- cast(x AS {SIGNED | UNSIGNED} [INT | INTEGER])
Description: Converts x to the BIGINT SIGNED or BIGINT UNSIGNED type.
Return type: BIGINT SIGNED or BIGINT UNSIGNED
Example:
1 2 3 4 5
gaussdb=# SELECT CAST(12 AS UNSIGNED); uint8 ------- 12 (1 row)
- hextoraw(text)
Description: Converts a string in hexadecimal format into raw type.
Return type: raw
Example:
1 2 3 4 5
gaussdb=# SELECT hextoraw('7D'); hextoraw ---------- 7D (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
gaussdb=# SELECT numtoday(2); numtoday ---------- 2 days (1 row)
- rawtohex(string)
Description: Converts a string in binary format into hexadecimal format.
The result is the ACSII code of the input characters in hexadecimal format.
Return type: varchar
Example:
1 2 3 4 5
gaussdb=# SELECT rawtohex('1234567'); rawtohex ---------------- 31323334353637 (1 row)
- to_blob(raw)
Description: Converts the RAW type to the BLOB type.
Return type: BLOB
Example:
1 2 3 4 5
gaussdb=# SELECT to_blob('0AADD343CDBBD'::RAW(10)); to_blob ---------------- 00AADD343CDBBD (1 row)
The to_blob function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_bigint(varchar)
Description: Converts the character type to the bigint type.
Return type: bigint
Example:
1 2 3 4 5
gaussdb=# SELECT to_bigint('123364545554455'); to_bigint ---------------- 123364545554455 (1 row)
- to_binary_double(expr)
Description: Converts an expression to a value of the float8 type.
expr: supports the number, float4, and float8 data types and character strings that can be implicitly converted to numeric types.
Return type: float8
Example:
1 2 3 4 5
gaussdb=# SELECT to_binary_double('12345678'); to_binary_double ------------------ 12345678 (1 row)
The to_binary_double function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_double(expr, fmt)
Description: Converts an expression to a value of the float8 type after format matching.
expr/fmt: supports character strings of the char, nchar, varchar2, and nvarchar2 types. The expr also supports numeric types that can be implicitly converted to character string types.
Return type: float8
Example:
1 2 3 4 5
gaussdb=# SELECT to_binary_double('1,2,3', '9,9,9'); to_binary_double ------------------ 123 (1 row)
The to_binary_double function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_double(expr default return_value on conversion error)
Description: Converts an expression to a value of the float8 type. If the conversion fails, the default value return_value is returned.
expr: supports the number, float4, and float8 data types and numeric types that can be implicitly converted to character strings. If expr is not of the numeric or character string type, an error message is displayed.
Return type: float8
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SELECT to_binary_double(1e2 default 12 on conversion error); to_binary_double ------------------ 100 (1 row) gaussdb=# SELECT to_binary_double('aa' default 12 on conversion error); to_binary_double ------------------ 12 (1 row)
The to_binary_double function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_double(expr default return_value on conversion error, fmt)
Description: Converts an expression to a value of the float8 type after format matching. If the expression fails to be converted, the default value return_value is returned.
expr/fmt: supports character strings of the char, nchar, varchar2, and nvarchar2 types. The expr also supports numeric types that can be implicitly converted to character string types.
Return type: float8
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SELECT to_binary_double('12-' default 10 on conversion error, '99S'); to_binary_double ------------------ -12 (1 row) gaussdb=# SELECT to_binary_double('aa-' default 12 on conversion error, '99S'); to_binary_double ------------------ 12 (1 row)
The to_binary_double function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_float(expr)
Description: Converts an expression to a value of the float4 type.
expr: supports the number, float4, and float8 data types and character strings that can be implicitly converted to numeric types.
Return type: float4
Example:
1 2 3 4 5
gaussdb=# SELECT to_binary_float('12345678'); to_binary_float ------------------ 1.23457e+07 (1 row)
The to_binary_float function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_float(expr, fmt)
Description: Converts an expression to a value of the float4 type after format matching.
expr/fmt: supports character strings of the char, nchar, varchar2, and nvarchar2 types. The expr also supports numeric types that can be implicitly converted to character string types.
Return type: float4
Example:
1 2 3 4 5
gaussdb=# SELECT to_binary_float('1,2,3', '9,9,9'); to_binary_float ------------------ 123 (1 row)
The to_binary_float function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_float(expr default return_value on conversion error)
Description: Converts an expression to a value of the float4 type. If the conversion fails, the default value return_value is returned.
expr: Supports the number, float4, and float8 numeric types and numeric types that can be implicitly converted to character strings. If expr is of a non-numeric or non-character string type, an error message is displayed.
Return type: float4
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SELECT to_binary_float(1e2 default 12 on conversion error); to_binary_float ------------------ 100 (1 row) gaussdb=# SELECT to_binary_float('aa' default 12 on conversion error); to_binary_float ------------------ 12 (1 row)
The to_binary_float function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_binary_float(expr default return_value on conversion error, fmt)
Description: Converts an expression to a value of the float4 type after format matching. If the expression fails to be converted, the default value return_value is returned.
expr/fmt: supports character strings of the char, nchar, varchar2, and nvarchar2 types. The expr also supports numeric types that can be implicitly converted to character string types.
Return type: float4
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SELECT to_binary_float('12-' default 10 on conversion error, '99S'); to_binary_float ------------------ -12 (1 row) gaussdb=# SELECT to_binary_float('aa-' default 12 on conversion error, '99S'); to_binary_float ------------------ 12 (1 row)
The to_binary_float function is supported only when the value of a_format_version is 10c and the value of a_format_dev_version is s2.
- to_char(datetime/interval [, fmt])
Description: Converts a DATETIME or INTERVAL value of the DATE/TIMESTAMP/TIMESTAMP WITH TIME ZONE/TIMESTAMP WITH LOCAL TIME ZONE type into the VARCHAR type according to the format specified by fmt.
- The optional parameter fmt allows for the following types: date, time, week, quarter, and century. Each type has a unique template. The templates can be combined together. Common templates include HH, MI, SS, YYYY, MM, and DD. For details, see Table 20.
- A template may have a modification word. FM is a common modification word and is used to suppress the preceding zero or the following blank spaces.
Return type: varchar
Example:
1 2 3 4 5
gaussdb=# SELECT to_char(current_timestamp,'HH12:MI:SS'); to_char ---------- 10:19:26 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_char(current_timestamp,'FMHH12:FMMI:FMSS'); to_char ---------- 10:19:46 (1 row)
- to_char(double precision/real, text)
Description: Converts the values of the floating point type into the strings in the specified format.
Return type: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_char(125.8::real, '999D99'); to_char --------- 125.80 (1 row)
- to_char (numeric/smallint/integer/bigint/double precision/real[, fmt])
Descriptions: Converts an integer or a value in floating point format into a string in specified format.
- The optional parameter fmt allows for the following types: decimal point, group (thousand) separator, positive/negative sign and currency sign. Each type has a unique template. The templates can be combined together. Common templates include: 9, 0, millesimal sign (,), and decimal point (.). For details, see Table 1.
- A template can have a modification word, similar to FM. However, FM does not suppress 0 which is output according to the template.
- Use the template X or x to convert an integer value into a string in hexadecimal format.
Return type: varchar
Example:
1 2 3 4 5
gaussdb=# SELECT to_char(1485,'9,999'); to_char --------- 1,485 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_char( 1148.5,'9,999.999'); to_char ------------ 1,148.500 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_char(148.5,'990999.909'); to_char ------------- 0148.500 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_char(123,'XXX'); to_char --------- 7B (1 row)
Table 1 fmt parameter of the number type Pattern
Description
, (comma)
Group (thousand) separator
. (period)
Decimal point
$
$ output to the specified position
0
Value with leading zeros
9
Value with specified digits
B
Space returned when the integer part is 0
C
Currency symbol (depending on the locale setting)
D
Decimal point (depending on the locale setting)
EEEE
Scientific notation
G
Group separator (depending on the locale setting)
L
Currency symbol (depending on the locale setting)
MI
Minus sign in the specified position (if the number is less than 0)
PR
Negative values in angle brackets
RN
Roman numeral (ranging from 1 to 3999)
S
Signed number (depending on the locale setting)
TM
Standard number in scientific notation
TM9
Standard number in scientific notation
TME
Standard number in scientific notation
U
Currency symbol (depending on the locale setting)
V
Decimal with specified number of digits shifted
PL
Plus sign in the specified position (if the number is greater than 0)
SG
Plus or minus sign in the specified position
TH or th
Ordinal number suffix
This function supports the $, C, TM, TM9, TME and U patterns when a_format_version is set to 10c and a_format_dev_version is set to s1. In addition, the fmt parameter cannot be set to TH, PL, or SG in this case.
- 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
gaussdb=# SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS'); to_char ---------- 15:02:12 (1 row)
- to_char(integer, text)
Description: Converts the values of the integer type into the strings in the specified format.
Return type: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_char(125, '999'); to_char --------- 125 (1 row)
- to_char(set)
Description: Converts a value of the SET type to a string. The distributed system does not support the SET data type.
Return value: text
- 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
gaussdb=# SELECT to_char(-125.8, '999D99S'); to_char --------- 125.80- (1 row)
- to_char (string)
Description: Converts the CHAR/VARCHAR/VARCHAR2/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
gaussdb=# SELECT to_char('01110'); to_char --------- 01110 (1 row)
- to_nvarchar2(numeric)
Description: Converts to the nvarchar2 type.
Parameter: numeric
Return type: nvarchar2
- 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
gaussdb=# SELECT to_char(current_timestamp, 'HH12:MI:SS'); to_char ---------- 10:55:59 (1 row)
- When a_format_version is set to 10c and a_format_dev_version is set to s1, the to_char function reports an error for the incorrect format (fmt).
- In non-compatible mode, the to_char function outputs the incorrect format (fmt) without change. For example, if the fmt is FF10, FF1 is matched for formatted output, and then 0 is output without change.
- to_nchar (datetime/interval [, fmt])
Description: Converts a DATETIME or INTERVAL value of the DATE/TIMESTAMP/TIMESTAMP WITH TIME ZONE/TIMESTAMP WITH LOCAL TIME ZONE type into the TEXT type according to the format specified by fmt.
- The optional parameter fmt allows for the following types: date, time, week, quarter, and century. Each type has a unique template. The templates can be combined together. Common templates include HH, MI, SS, YYYY, MM, and DD. For details, see Table 20.
- A template may have a modification word. FM is a common modification word and is used to suppress the preceding zero or the following blank spaces.
Return type: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_nchar(current_timestamp,'HH12:MI:SS'); to_nchar ---------- 10:19:26 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_nchar(current_timestamp,'FMHH12:FMMI:FMSS'); to_nchar ---------- 10:19:46 (1 row)
- to_nchar(double precision/real, text)
Description: Converts the values of the floating point type into the strings in the specified format.
Return type: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_nchar(125.8::real, '999D99'); to_nchar --------- 125.80 (1 row)
- to_nchar (numeric/smallint/integer/bigint/double precision/real[, fmt])
Descriptions: Converts an integer or a value in floating point format into a string in specified format.
- The optional parameter fmt allows for the following types: decimal point, group (thousand) separator, positive/negative sign and currency sign. Each type has a unique template. The templates can be combined together. Common templates include: 9, 0, millesimal sign (,), and decimal point (.). For details, see Table 1.
- A template can have a modification word, similar to FM. However, FM does not suppress 0 which is output according to the template.
- Use the template X or x to convert an integer value into a string in hexadecimal format.
Return type: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_nchar(1485,'9,999'); to_nchar --------- 1,485 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_nchar( 1148.5,'9,999.999'); to_nchar ------------ 1,148.500 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_nchar(148.5,'990999.909'); to_nchar ------------- 0148.500 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_nchar(123,'XXX'); to_nchar --------- 7B (1 row)
This function supports the $, C, TM, TM9, TME and U patterns when a_format_version is set to 10c and a_format_dev_version is set to s1. In addition, the fmt parameter cannot be set to TH, PL, or SG in this case.
- to_nchar(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
gaussdb=# SELECT to_nchar(interval '15h 2m 12s', 'HH24:MI:SS'); to_nchar ---------- 15:02:12 (1 row)
- to_nchar(integer, text)
Description: Converts the values of the integer type into the strings in the specified format.
Return type: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_nchar(125, '999'); to_nchar --------- 125 (1 row)
- to_nchar(set)
Description: Converts a value of the SET type to a string. The distributed system does not support the SET data type.
Return value: text
- to_nchar(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
gaussdb=# SELECT to_nchar(-125.8, '999D99S'); to_nchar --------- 125.80- (1 row)
- to_nchar (string)
Description: Converts the CHAR/VARCHAR/VARCHAR2/CLOB type into the TEXT 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: text
Example:
1 2 3 4 5
gaussdb=# SELECT to_nchar('01110'); to_nchar --------- 01110 (1 row)
- to_nchar(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
gaussdb=# SELECT to_nchar(current_timestamp, 'HH12:MI:SS'); to_nchar ---------- 10:55:59 (1 row)
- to_clob(char/nchar/varchar/nvarchar/varchar2/nvarchar2/text/raw)
Description: Converts the raw type or text character set type CHAR, NCHAR, VARCHAR, VARCHAR2, NVARCHAR, NVARCHAR2, or TEXT to the CLOB type.
Return type: CLOB
Example:
1 2 3 4 5
gaussdb=# SELECT to_clob('ABCDEF'::RAW(10)); to_clob --------- ABCDEF (1 row)
1 2 3 4 5
gaussdb=# SELECT to_clob('hello111'::CHAR(15)); to_clob ---------- hello111 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_clob('gauss123'::NCHAR(10)); to_clob ---------- gauss123 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_clob('gauss234'::VARCHAR(10)); to_clob ---------- gauss234 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_clob('gauss345'::VARCHAR2(10)); to_clob ---------- gauss345 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_clob('gauss456'::NVARCHAR2(10)); to_clob ---------- gauss456 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_clob('World222!'::TEXT); to_clob ----------- World222! (1 row)
- to_date(text)
Description: Converts values of the text type into the timestamp in the specified format.
- Format 1: Date without separators, for example, 20150814. The value must contain the complete year, month, and day.
- Format 2: Date with separators, for example, 2014-08-14. The separator can be any non-digit character.
Return type: timestamp without time zone
Example:
1 2 3 4 5
gaussdb=# SELECT to_date('2015-08-14'); to_date --------------------- 2015-08-14 00:00:00 (1 row)
Case execution environment: The value of a_format_version is 10c, the value of a_format_dev_version is s1, and the value of nls_timestamp_format is YYYY-MM-DD HH24:MI:SS.
- to_date(text, text)
Description: Converts the values of the string type into the dates in the specified format.
Return type: timestamp without time zone
Example:
1 2 3 4 5
gaussdb=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY'); to_date --------------------- 2000-12-05 00:00:00 (1 row)
- to_date(text [DEFAULT return_value ON CONVERSION ERROR [, fmt]])
Description: Converts a string text into a value of the DATE type according to the format specified by fmt. If fmt is not specified, a_format_version is set to 10c, and a_format_dev_version is set to s1, the format specified by nls_timestamp_format is used for conversion. Otherwise, the fixed format (fmt = 'yyyy-mm-dd hh24-mi-ss') is used for conversion.
- text: any expression that can be calculated to CHAR/VARCHAR2/NCHAR/NVARCHAR2/TEXT string. If null is entered, null is returned.
- DEFAULT return_value ON CONVERSION ERROR: This parameter is optional, which can be used to specify the return value when text fails to be converted to the DATE type. The value of return_value can be an expression or a bound variable that can be converted to the CHAR/VARCHAR2/NCHAR/NVARCHAR2/TEXT type or null. The method of converting return_value to the DATE type is the same as that of converting text to the DATE type. If return_value fails to be converted to the DATE type, an error is reported.
- fmt: This parameter is optional, which specifies the date and time model format of text. By default, text must comply with the default date format. If fmt is set to J, text must be an integer.
Return type: timestamp without time zone
Example:
1 2 3 4 5 6 7 8 9 10
gaussdb=# SELECT to_date('2015-08-14'); to_date --------------------- 2015-08-14 00:00:00 (1 row) gaussdb=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY'); to_date --------------------- 2000-12-05 00:00:00 (1 row)
gaussdb=# SET a_format_version='10c'; SET gaussdb=# SET a_format_dev_version='s1'; SET gaussdb=# SHOW nls_timestamp_format; nls_timestamp_format ---------------------------- DD-Mon-YYYY HH:MI:SS.FF AM (1 row) gaussdb=# SELECT to_date('12-jan-2022' default '12-apr-2022' on conversion error); to_date --------------------- 2022-01-12 00:00:00 (1 row) gaussdb=# SELECT to_date('12-ja-2022' default '12-apr-2022' on conversion error); to_date --------------------- 2022-04-12 00:00:00 (1 row) gaussdb=# SELECT to_date('2022-12-12' default '2022-01-01' on conversion error, 'yyyy-mm-dd'); to_date --------------------- 2022-12-12 00:00:00 (1 row)
- The DEFAULT return_value ON CONVERSION ERROR syntax is supported only when a_format_version is set to 10c and a_format_dev_version is set to s1.
- When a_format_version is set to 10c and a_format_dev_version is set to s1, the system may not report an error if the entered year exceeds 9999. For example, the result of to_date('99999-12-12', 'yyyy-mm-dd hh24:mi:ss') is 9999-09-12 12:00:00. If the value of year exceeds 9999, the number following 9999 will be parsed as the next fmt. This restriction also applies to to_timestamp.
- to_number ( expr [, fmt])
Description: Converts expr into a value of the NUMBER type according to the specified format.
For details about the type conversion formats, see Table 3.
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 a sign-free number.
During the conversion from a hexadecimal string to a decimal digit, the format string cannot have a character other than x or X. Otherwise, an error is reported.
Return type: number
Example:
1 2 3 4 5
gaussdb=# SELECT to_number('12,454.8-', '99G999D9S'); to_number ----------- -12454.8 (1 row)
- to_number(text, text)
Description: Converts the values of the string type into the numbers in the specified format.
Return type: numeric
Example:
1 2 3 4 5
gaussdb=# SELECT to_number('12,454.8-', '99G999D9S'); to_number ----------- -12454.8 (1 row)
- to_number(expr [DEFAULT return_value ON CONVERSION ERROR [, fmt]])
Description: Converts a string expr to a value of the numeric type based on the format specified by fmt. If fmt is not specified, text must be a character string that can be directly converted to numeric, for example, '123', '1e2'.
For details about the type conversion formats, see Table 4.
- expr: an expression that can be converted into a CHAR/VARCHAR2/NCHAR/NVARCHAR2/TEXT/INT/FLOAT string. If null is entered, null is returned.
- DEFAULT return_value ON CONVERSION ERROR: This parameter is optional, which can be used to specify the return value when expr fails to be converted to the numeric type. Similar to expr, return_value can be any type that can be converted to a character string. Similar to expr, return_value is converted based on the format specified by fmt. The system checks whether return_value fails to be converted. If return_value fails to be converted, the function reports an error.
- fmt: This parameter is optional, which specifies the conversion format of expr.
If any input parameter is NULL, NULL is returned.
Return type: numeric
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
gaussdb=# SET a_format_version='10c'; gaussdb=# SET a_format_dev_version='s1'; gaussdb=# SELECT to_number('1e2'); to_number ----------- 100 (1 row) gaussdb=# SELECT to_number('123.456'); to_number ----------- 123.456 (1 row) gaussdb=# SELECT to_number('123', '999'); to_number ----------- 123 (1 row) gaussdb=# SELECT to_number('123-', '999MI'); to_number ----------- -123 (1 row) gaussdb=# SELECT to_number('123' default '456-' on conversion error, '999MI'); to_number ----------- -456 (1 row)
The DEFAULT return_value ON CONVERSION ERROR syntax is supported only when a_format_version is set to 10c and a_format_dev_version is set to s1.
- to_timestamp(double precision)
Description: Converts a UNIX century into a timestamp.
Return type: timestamp with time zone
Example:
1 2 3 4 5
gaussdb=# SELECT to_timestamp(1284352323); to_timestamp ------------------------ 2010-09-13 12:32:03+08 (1 row)
- to_timestamp(string [,fmt])
Description: Converts a string into a value of the timestamp type according to the format specified by fmt. When fmt is not specified, perform the conversion according to the format specified by nls_timestamp_format.
In to_timestamp in GaussDB:
- If the input year YYYY is 0, an error will be reported.
- If the input year YYYY is less than 0, specify SYYYY in fmt. The year with the value of n (an absolute value) BC will be output correctly.
Characters in the fmt must match the schema for formatting the data and time. Otherwise, an error is reported.
Return type: timestamp without time zone
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SHOW nls_timestamp_format; nls_timestamp_format ---------------------------- DD-Mon-YYYY HH:MI:SS.FF AM (1 row) gaussdb=# SELECT to_timestamp('12-sep-2014'); to_timestamp --------------------- 2014-09-12 00:00:00 (1 row)
1 2 3 4 5
gaussdb=# 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
gaussdb=# SELECT to_timestamp('-1','SYYYY'); to_timestamp ------------------------ 0001-01-01 00:00:00 BC (1 row)
1 2 3 4 5
gaussdb=# SELECT to_timestamp('98','RR'); to_timestamp --------------------- 1998-01-01 00:00:00 (1 row)
1 2 3 4 5
gaussdb=# SELECT to_timestamp('01','RR'); to_timestamp --------------------- 2001-01-01 00:00:00 (1 row)
- When a_format_version is set to 10c and a_format_dev_version is set to s1, fmt supports FF[7-9]. When FF[7-9] is used, and the length of the corresponding position in the string is less than or equal to the number following FF, then, the number can be converted. However, the maximum length of the final conversion result is six digits.
- The result returned by the current_timestamp function cannot be used as a string parameter.
- to_timestamp(text [DEFAULT return_value ON CONVERSION ERROR [, fmt]])
Description: Converts a string text into a value of the DATE type according to the format specified by fmt. If fmt is not specified, a_format_version is set to 10c, and a_format_dev_version is set to s1, the format specified by nls_timestamp_format is used for conversion. Otherwise, the fixed format (fmt = 'yyyy-mm-dd hh24-mi-ss') is used for conversion.
- text: any expression that can be calculated to CHAR/VARCHAR2/NCHAR/NVARCHAR2/TEXT string. If null is entered, null is returned.
- DEFAULT return_value ON CONVERSION ERROR: This parameter is optional, which can be used to specify the return value when text fails to be converted to the DATE type. The value of return_value can be an expression or a bound variable that can be converted to the CHAR/VARCHAR2/NCHAR/NVARCHAR2/TEXT type or null. The method of converting return_value to the timestamp type is the same as that of converting text to the timestamp type. If return_value fails to be converted to the timestamp type, an error is reported.
- fmt: This parameter is optional, which specifies the date and time model format of text. By default, text must comply with the default date format. If fmt is set to J, text must be an integer.
Return type: timestamp without time zone
Example:
gaussdb=# set a_format_version='10c'; SET gaussdb=# set a_format_dev_version='s1'; SET gaussdb=# SELECT to_timestamp('11-Sep-11' DEFAULT '12-Sep-10 14:10:10.123000' ON CONVERSION ERROR,'DD-Mon-YY HH24:MI:SS.FF'); to_timestamp --------------------- 2011-09-11 00:00:00 (1 row) gaussdb=# SELECT to_timestamp('12-Sep-10 14:10:10.123000','DD-Mon-YY HH24:MI:SSXFF'); to_timestamp ------------------------- 2010-09-12 14:10:10.123 (1 row)
The DEFAULT return_value ON CONVERSION ERROR syntax is supported only when a_format_version is set to 10c and a_format_dev_version is set to s1.
- to_timestamp(text, text)
Description: Converts values of the string type into the timestamp of the specified type.
Return type: timestamp
Example:
1 2 3 4 5
gaussdb=# SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY'); to_timestamp --------------------- 2000-12-05 00:00:00 (1 row)
- to_timestamp_tz(string [DEFAULT return_value ON CONVERSION ERROR] [,fmt])
Description: Converts a string into a value of the timestamp type with the time zone according to the format specified by fmt. When fmt is not specified, perform the conversion according to the format specified by nls_timestamp_tz_format.
DEFAULT return_value ON CONVERSION ERROR: This parameter is optional, If string fails to be converted to the timestamp type with time zone, return_value is converted to the timestamp type with time zone.
fmt: This parameter is optional, which specifies the date and time model format of string. The setting of this parameter is the same as that in the to_timestamp function.
Return type: timestamp with time zone
Example:
1 2 3 4 5
gaussdb=# SELECT to_timestamp_tz('05 DeX 2000' DEFAULT '05 Dec 2001' ON CONVERSION ERROR, 'DD Mon YYYY'); to_timestamp_tz -------------------------- 2001-12-05 00:00:00+08:00 (1 row)
This function is valid only when a_format_version is set to 10c and a_format_dev_version is set to s1.
- to_timestamp_tz(string [DEFAULT return_value ON CONVERSION ERROR], fmt, nlsparam)
Description: Converts a string into a value of the timestamp type with the time zone according to the format specified by fmt. If string fails to be converted to the timestamp type with time zone, return_value is converted to the timestamp type with time zone. nlsparam specifies the language of the month and day in the time character string. The format is 'nls_date_language=language'. Currently, language supports only ENGLISH and AMERICAN. Currently, the result of correctly using the nlsparam parameter is the same as that of omitting the nlsparam parameter. For details, see Table 2.
Return type: timestamp with time zone
Table 2 Parameters Parameter
Type
Description
string
text
Converts to a string of the timestamp with time zone type.
return_value
text
If a string fails to be converted to the timestamp type with time zone, return_value is converted to the timestamp type with time zone.
fmt
text
Specifies the date and time model format of the string parameter.
nlsparam
text
Specifies the language of the month and day in the string parameter.
Example:
1 2 3 4 5
gaussdb=# SELECT to_timestamp_tz('05 DeX 2000' DEFAULT '05 Dec 2001' ON CONVERSION ERROR, 'DD Mon YYYY','nls_date_language=AMERICAN'); to_timestamp_tz -------------------------- 2001-12-05 00:00:00+08:00 (1 row)
This function is valid only when a_format_version is set to 10c and a_format_dev_version is set to s4.
- to_dsinterval(text)
Description: Converts characters to the interval type. SQL-compatible and ISO formats are supported.
Return type: interval
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SELECT to_dsinterval('12 1:2:3.456'); to_dsinterval ---------------------- 12 days 01:02:03.456 (1 row) gaussdb=# SELECT to_dsinterval('P3DT4H5M6S'); to_dsinterval ----------------- 3 days 04:05:06 (1 row)
This function is valid only when a_format_version is set to 10c and a_format_dev_version is set to s2.
- to_yminterval(text)
Description: Converts characters to the interval type. SQL-compatible and ISO formats are supported.
Return type: interval
Example:
1 2 3 4 5 6 7 8 9 10 11
gaussdb=# SELECT to_yminterval('1-1'); to_yminterval ----------------- 1 year 1 mon (1 row) gaussdb=# SELECT to_yminterval('P13Y3M4DT4H2M5S'); to_yminterval ----------------- 13 years 3 mons (1 row)
This function is valid only when a_format_version is set to 10c and a_format_dev_version is set to s2.
Pattern |
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 |
Signed number (depending on the locale setting) |
L |
Currency symbol (depending on the locale setting) |
D |
Decimal point (depending on the locale setting) |
G |
Group separator (depending on the locale setting) |
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 numeral (ranging from 1 to 3999) |
TH or th |
Ordinal number suffix |
V |
Decimal with specified number of digits shifted |
x or X |
Hexadecimal-to-decimal conversion identifier |
Pattern |
Description |
---|---|
9 |
Matches a digit. The number of digits "9" can be greater than or equal to that in the corresponding position in expr. |
0 |
Strictly matches a digit. The number of digits "0" must be equal to that in expr. |
5 |
Matches digit 0 or 5. |
Period (.) |
Decimal point in the specified position |
Comma (,) |
Group (thousand) separator in the specified position. Multiple commas can be specified in fmt. |
B |
No leading sign |
PR |
A negative value in angle brackets, or a positive value with no leading sign |
S |
A negative value with the leading minus sign (–) or a positive value with the leading plus sign (+) |
MI |
A negative value with the leading minus sign (–) or a positive value with no leading sign |
$ |
Leading dollar sign |
L |
Local currency symbol |
C |
Symbol of currency in the specified position (complying with the ISO standard) |
U |
Dual-currency symbol |
D |
Decimal point (uses locale) |
G |
Group separator (complying with the ISO standard). Multiple commas can be specified in fmt. |
RN / rn |
Roman numeral (ranging from 1 to 3999). to_number does not support this format. |
V |
to_number does not support this format. |
X / x |
Conversion between hexadecimal and decimal |
TM |
to_number does not support this format. |
FM |
This format can be used only at the beginning of fmt. |
EEEE |
Conversion based on the scientific notation model |
When a_format_version is set to 10c and a_format_dev_version is set to s1, refer to this table for the setting of fmt. Otherwise, refer to the previous table. The fmt setting complying with the ISO standard is affected by the values of LC_MONETARY and LC_NUMERIC parameters.
- cast_varchar2_to_raw_for_histogram(varchar2)
Description: Converts from the varchar2 type to the raw type.
Return type: raw
- abstime_to_smalldatetime(abstime)
Description: Converts abstime to smalldatetime.
Parameter: abstime
Return type: smalldatetime
- bpchar_date(character)
Description: Converts a string to a date.
Parameter: character
Return type: date
- bpchar_float4(character)
Description: Converts a string to float4.
Parameter: character
Return type: real
- bpchar_float8(character)
Description: Converts a string to float8.
Parameter: character
Return type: double precision
- bpchar_int4(character)
Description: Converts a string to int4.
Parameter: character
Return type: integer
- bpchar_int8(character)
Description: Converts a string to int8.
Parameter: character
Return type: bigint
- bpchar_numeric(character)
Description: Converts a string to numeric.
Parameter: character
Return type: numeric
- bpchar_timestamp(character)
Description: Converts a string to a timestamp.
Parameter: character
Return type: timestamp without time zone
- bpchar_to_smalldatetime(character)
Description: Converts a string to smalldatetime.
Parameter: character
Return type: smalldatetime
- complex_array_in(cstring, oid, int2vector)
Description: Converts the external complex_array type to the internal anyarray array type.
Parameter: cstring, oid, int2vector
Return type: anyarray
- date_bpchar(date)
Description: Converts the date type to bpchar.
Parameter: date
Return type: character
- date_varchar(date)
Description: Converts date to varchar.
Parameter: date
Return type: character varying
- f8toi1(double precision)
Description: Forcibly converts float8 to uint8.
Parameter: double precision
Return type: tinyint
- float4_varchar(real)
Description: Converts float4 to varchar.
Parameter: real
Return type: character varying
- float8_bpchar(double precision)
Description: Converts float8 to bpchar.
Parameter: double precision
Return type: character
- float8_interval(double precision)
Description: Converts float8 to interval.
Parameter: double precision
Return type: interval
- float8_text(double precision)
Description: Converts float8 to text.
Parameter: double precision
Return type: text
- float8_varchar(double precision)
Description: Converts float8 to varchar.
Parameter: double precision
Return type: character varying
- i1tof8(tinyint)
Description: Converts uint8 to float8.
Parameter: tinyint
Return type: double precision
- int1_avg_accum(bigint[], tinyint)
Description: Adds the second parameter of the uint8 type to the first parameter. The first parameter is an array of the bigint type.
Parameter: bigint[], tinyint
Return type: bigint[]
- int1_bpchar(tinyint)
Description: Converts uint8 to bpchar.
Parameter: tinyint
Return type: character
- int1_mul_cash(tinyint, money)
Description: Returns the product of a parameter of the int8 type and a parameter of the cash type. The return type is cash.
Parameter: tinyint, money
Return type: money
- int1_numeric(tinyint)
Description: Converts uint8 to numeric.
Parameter: tinyint
Return type: numeric
- int1_nvarchar2(tinyint)
Description: Converts uint8 to nvarchar2.
Parameter: tinyint
Return type: nvarchar2
- int1_varchar(tinyint)
Description: Converts uint8 to varchar.
Parameter: tinyint
Return type: character varying
- int1in(cstring)
Description: Converts a string into an unsigned 1-byte integer.
Parameter: cstring
Return type: tinyint
- int1out(tinyint)
Description: Converts an unsigned 1-byte integer into a string.
Parameter: tinyint
Return type: cstring
- int1up(tinyint)
Description: Converts an input integer to an unsigned 1-byte integer.
Parameter: tinyint
Return type: tinyint
- int2_bool(smallint)
Description: Converts a signed two-byte integer to the bool type.
Parameter: smallint
Return type: Boolean
- int2_bpchar(smallint)
Description: Converts a signed two-byte integer to the bpchar type.
Parameter: smallint
Return type: character
- int2_text(smallint)
Description: Converts a signed two-byte integer to the text type.
Parameter: smallint
Return type: text
- int2_varchar(smallint)
Description: Converts a signed two-byte integer to the varchar type.
Parameter: smallint
Return type: character varying
- int4_bpchar(integer)
Description: Converts a signed four-byte integer to bpchar.
Parameter: integer
Return type: character
- int4_text(integer)
Description: Converts a signed four-byte integer to the text type.
Parameter: integer
Return type: text
- int4_varchar(integer)
Description: Converts a signed four-byte integer into varchar.
Parameter: integer
Return type: character varying
- int8_bool(bigint)
Description: Converts an eight-byte signed integer to a Boolean value.
Parameter: bigint
Return type: Boolean
- int8_bpchar(bigint)
Description: Converts an 8-byte signed integer to bpchar.
Parameter: bigint
Return type: character
- int8_text(bigint)
Description: Converts an eight-byte signed integer to the text type.
Parameter: bigint
Return type: text
- int8_varchar(bigint)
Description: Converts an eight-byte signed integer to varchar.
Parameter: bigint
Return type: character varying
- intervaltonum(interval)
Description: Converts the internal dats type date to numeric.
Parameter: interval
Return type: numeric
- numeric_bpchar(numeric)
Description: Converts numeric to bpchar.
Parameter: numeric
Return type: character
- numeric_int1(numeric)
Description: Converts numeric to a signed one-byte integer.
Parameter: numeric
Return type: tinyint
- numeric_varchar(numeric)
Description: Converts numeric to varchar.
Parameter: numeric
Return type: character varying
- nvarchar2in(cstring, oid, integer)
Description: Converts c string to varchar.
Parameter: cstring, oid, integer
Return type: nvarchar2
- nvarchar2out(nvarchar2)
Description: Converts text into a c string.
Parameter: nvarchar2
Return type: cstring
- nvarchar2send(nvarchar2)
Description: Converts varchar to binary.
Parameter: nvarchar2
Return type: bytea
- oidvectorin_extend(cstring)
Description: Converts a string to oidvector.
Parameter: cstring
Return type: oidvector_extend
- oidvectorout_extend(oidvector_extend)
Description: Converts oidvector to a string.
Parameter: oidvector_extend
Return type: cstring
- oidvectorsend_extend(oidvector_extend)
Description: Converts oidvector to a string.
Parameter: oidvector_extend
Return type: bytea
- text_date(text)
Description: Converts the text type to the date type.
Parameter: text
Return type: date
- text_float8(text)
Description: Converts the text type to float8.
Parameter: text
Return type: double precision
- text_int2(text)
Description: Converts the text type to the int2 type.
Parameter: text
Return type: smallint
- text_int8(text)
Description: Converts the text type to the int8 type.
Parameter: text
Return type: bigint
- text_numeric(text)
Description: Converts the text type to the numeric type.
Parameter: text
Return type: numeric
- text_timestamp(text)
Description: Converts the text type to the timestamp type.
Parameter: text
Return type: timestamp without time zone
- time_text(time without time zone)
Description: Converts the time type to the text type.
Parameter: time without time zone
Return type: text
- timestamp_text(timestamp without time zone)
Description: Converts the timestamp type to the text type.
Parameter: timestamp without time zone
Return type: text
- timestamp_to_smalldatetime(timestamp without time zone)
Description: Converts the timestamp type to the smalldatetime type.
Parameter: timestamp without time zone
Return type: smalldatetime
- timestamp_varchar(timestamp without time zone)
Description: Converts the timestamp type to varchar.
Parameter: timestamp without time zone
Return type: character varying
- timestamptz_to_smalldatetime(timestamp with time zone)
Description: Converts timestamptz to smalldatetime.
Parameter: timestamp with time zone
Return type: smalldatetime
- timestampzone_text(timestamp with time zone)
Description: Converts the timestampzone type to the text type.
Parameter: timestamp with time zone
Return type: text
- timetz_text(time with time zone)
Description: Converts the timetz type to the text type.
Parameter: time with time zone
Return type: text
- to_integer(character varying)
Description: Converts data to the integer type.
Parameter: character varying
Return type: integer
- to_interval(character varying)
Description: Converts to the interval type.
Parameter: character varying
Return type: interval
- to_numeric(character varying)
Description: Converts to the numeric type.
Parameter: character varying
Return type: numeric
- to_ts(character varying)
Description: Converts to the ts type.
Parameter: character varying
Return type: timestamp without time zone
- to_varchar2(timestamp without time zone)
Description: Converts to the varchar2 type.
Parameter: timestamp without time zone
Return type: character varying
- varchar_date(character varying)
Description: Converts varchar to date.
Parameter: character varying
Return type: date
- varchar_float4(character varying)
Description: Converts varchar to float4.
Parameter: character varying
Return type: real
- varchar_float8(character varying)
Description: Converts the varchar type to the float8 type.
Parameter: character varying
Return type: double precision
- varchar_int4(character varying)
Description: Converts the type from varchar to int4.
Parameter: character varying
Return type: integer
- varchar_int8(character varying)
Description: Converts the varchar type to the int8 type.
Parameter: character varying
Return type: bigint
- varchar_numeric(character varying)
Description: Converts varchar to numeric.
Parameter: character varying
Return type: numeric
- varchar_timestamp(character varying)
Description: Converts varchar to timestamp.
Parameter: character varying
Return type: timestamp without time zone
- varchar2_to_smlldatetime(character varying)
Description: Converts varchar2 to smlldatetime.
Parameter: character varying
Return type: smalldatetime
- xidout4(xid32)
Description: The xid output is a four-byte number.
Parameter: xid32
Return type: cstring
- xidsend4(xid32)
Description: Converts xid to the binary format.
Parameter: xid32
Return type: bytea
- treat(expr AS [JSON | REF] schema.type)
Description: Converts expr to the type (JSON or user-defined type) specified by the keyword after AS.
Return value type: JSON or user-defined type.
Example:
1 2 3 4 5 6 7 8 9
gaussdb=# CREATE TABLE json_doc(data CLOB); gaussdb=# INSERT INTO json_doc values('{"name":"a"}'); gaussdb=# SELECT treat(data as json) FROM json_doc; json -------------- {"name":"a"} (1 row) gaussdb=# DROP TABLE json_doc; DROP TABLE
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot