Syntax
CAST(expr AS type)
Purpose
The CAST() function explicitly converts an expression from one data type to another, converting the expr field value to the type data type.
Parameters
The parameters are described as follows:
expr: any valid SQL expression.AS: separates the two parameters. The part beforeASis the data to be processed, and the part afterASis the target data type.type: the data type provided by the target system. Supported types include:CHAR[(N)] [CHARACTER SET charset_name]Generates a string of the
CHARdata type. If the expressionexpris empty (length 0), the result type isCHAR(0). If an optional lengthNis specified,CHAR(N)forces the conversion to use no more thanNcharacters; values shorter thanNcharacters are not padded. If no optional lengthNis specified, OceanBase Database calculates the maximum length based on the expression. If theCHARACTER SET charset_nameclause is not specified,CHARgenerates a string with the default character set.DATE: generates aDATEvalue.DATETIME [ (M) ]: generates aDATETIMEvalue. TheMvalue is optional and specifies the precision of seconds in decimal places, ranging from [0,6].DECIMAL [ (M [,D] ) ]: generates aDECIMALvalue. TheMandDvalues are optional and specify the maximum number of digits (precision) and the number of digits after the decimal point (scale), respectively. The maximum value ofMis 65, and the maximum value ofDis 30. IfDis omitted, the default is0; ifMis omitted, the default is10.SIGNED [INTEGER]: generates a signedBIGINTvalue.JSON: generates aJSONvalue. For conversion rules between JSON and other data types, see JSON data type conversion.TIME [ (M) ]: generates aTIMEvalue. If an optionalMvalue is provided, it specifies the precision of seconds in decimal places.UNSIGNED [INTEGER]: generates an unsignedBIGINTvalue.DECIMAL(m,d): a fixed-point number type, wheremrepresents the total number of digits anddrepresents the number of decimal places.FLOAT: a single-precision floating-point number, suitable for approximate numeric values.BIGINT: explicitly converts the value of the expressionexprto theBIGINTtype.Note
This type was introduced in OceanBase Database V4.6.0.
INTorINTEGER: a 32-bit signed integer.SMALLINT: a 16-bit signed integer.TINYINT: an 8-bit signed integer.ARRAY: converts elements in aJSONarray to an array of a specific type. Supports theARRAY<element_type>format for declaring array element types and allows nested arrays, such asARRAY<ARRAY<INT>>, for representing multidimensional array structures.
Considerations
When using the CAST function for data type conversion, the following scenarios are supported:
The data types of the two expressions are identical.
The two expressions can be implicitly converted.
The data type must be explicitly converted.
If the numeric value of the expression
exprexceeds the numeric range of the target data type, the type conversion will returnnull.Converting a high-precision data type to a low-precision data type will result in a loss of precision.
Converting a fixed-point number (
DECIMAL(m,d)) or a floating-point number (DOUBLE,FLOAT) to an integer (INT/INTEGER,SMALLINT, orTINYINT) will result in a loss of precision.If the expression
expris of theVARCHARtype and not a numeric value, converting it toINT/INTEGER,SMALLINT, orTINYINTwill return0.If the expression
expris of the JSON type and not a numeric value, converting it toINT/INTEGER,SMALLINT, orTINYINTwill result in an error.If the
VARCHARorJSONdata does not conform to theARRAYformat, an error will occur during conversion.
If an impossible conversion is attempted, OceanBase Database will display an error message. If the length of the target data type is not specified during conversion, the maximum length supported by the OceanBase Database system is used. For example, VARCHAR supports up to 262,143 bytes, and NUMBER supports up to 65 bits of floating-point precision.
The CAST() function supports operations with signed and unsigned 64-bit values. If you are using a numeric operator (e.g., +) and one of the operands is an unsigned integer, the result will be unsigned. You can explicitly declare the result using SIGNED or UNSIGNED to specify whether the operation should be treated as a signed or unsigned 64-bit integer. If either operand is a floating-point value, the result will be a floating-point value.
Examples
Convert the value 0 to the
DATEtype.obclient> SELECT CAST(0 AS DATE); +-----------------+ | CAST(0 AS DATE) | +-----------------+ | 0000-00-00 | +-----------------+ 1 row in setConvert the value 123 to the
TIMEtype.obclient> SELECT CAST(123 AS TIME); +-------------------+ | CAST(123 AS TIME) | +-------------------+ | 00:01:23 | +-------------------+ 1 row in setConvert the value 123 to the
DATETIMEtype.obclient> SELECT CAST(123 AS DATETIME(4)); +--------------------------+ | CAST(123 AS DATETIME(4)) | +--------------------------+ | 2000-01-23 00:00:00.0000 | +--------------------------+ 1 row in setConvert the value 123 to the
DECIMALtype.obclient> SELECT CAST(123 AS DECIMAL(3,2)); +---------------------------+ | CAST(123 AS DECIMAL(3,2)) | +---------------------------+ | 9.99 | +---------------------------+ 1 row in setConvert the string "123" to the JSON type.
obclient> SELECT CAST("123" AS JSON); +---------------------+ | CAST("123" AS JSON) | +---------------------+ | 123 | +---------------------+ 1 row in setConvert the result of "1-2" to an unsigned and a signed representation, respectively.
obclient> SELECT CAST(1-2 AS UNSIGNED), CAST(cast(1-2 AS UNSIGNED) AS SIGNED); +-----------------------+---------------------------------------+ | CAST(1-2 AS UNSIGNED) | CAST(cast(1-2 AS UNSIGNED) AS SIGNED) | +-----------------------+---------------------------------------+ | 18446744073709551615 | -1 | +-----------------------+---------------------------------------+ 1 row in setPerform numeric operations using the
CAST()function.obclient> SELECT CAST(1 AS UNSIGNED) - 2.0; +---------------------------+ | CAST(1 AS UNSIGNED) - 2.0 | +---------------------------+ | -1.0 | +---------------------------+ 1 row in setConvert the value 123 to the
CHARtype.obclient> SELECT CAST(123 AS CHAR(2)); +----------------------+ | CAST(123 AS CHAR(2)) | +----------------------+ | 12 | +----------------------+ 1 row in setConvert the value 1 to the
CHARtype and specify the character set.obclient> SELECT CAST(1 AS CHAR CHARACTER SET utf8mb4); +---------------------------------------+ | CAST(1 AS CHAR CHARACTER SET utf8mb4) | +---------------------------------------+ | 1 | +---------------------------------------+ 1 row in setConvert the character value
123to an integer and convert the integer1to theINTtype.obclient> SELECT CAST('123' AS INT),CAST(1 AS CHAR(10)); +--------------------+---------------------+ | CAST('123' AS INT) | CAST(1 AS INT) | +--------------------+---------------------+ | 123 | 1 | +--------------------+---------------------+ 1 row in set (0.035 secConvert the
JSONarray [1,2,3] to anARRAYofINTtype, as shown in the following statement:obclient> SELECT CAST( JSON '[1,2,3]' AS ARRAY<INT>); +-------------------------------------+ | CAST( JSON '[1,2,3]' AS ARRAY<INT> | +-------------------------------------+ | [1,2,3] | +-------------------------------------+ 1 row in setConvert the nested
JSONarray [[1,2],[3,4]] to a nestedARRAY<ARRAY<INT>>type, as shown in the following statement:obclient> SELECT CAST('[[1,2],[3,4]]' AS ARRAY<ARRAY<INT>>); +--------------------------------------------+ | CAST('[[1,2],[3,4]]' AS ARRAY<ARRAY<INT>>) | +--------------------------------------------+ | [[1,2],[3,4]] | +--------------------------------------------+ 1 row in setConvert the floating-point value
123.456to theBIGINTtype.obclient> SELECT CAST(123.456 AS BIGINT);The return result is as follows:
+-------------------------+ | CAST(123.456 AS BIGINT) | +-------------------------+ | 123 | +-------------------------+ 1 row in set
