Syntax
CAST(expr AS type)
Purpose
The CAST() function explicitly converts an expression from one data type to another. It converts the value of the expr field to the type data type.
Parameters
The parameters are described as follows:
expr: an expression of any valid SQL type.AS: separates the two parameters. The data beforeASis the data to be processed, and the data 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 with the
CHARdata type. If the expressionexpris empty (length 0), the result type isCHAR(0). If an optional lengthNis specified,CHAR(N)will convert the string to use no more thanNcharacters. Values shorter thanNcharacters will not be padded. If no optional lengthNis specified, OceanBase Database will calculate the maximum length based on the expression. If theCHARACTER SET charset_nameclause is not specified,CHARwill generate a string with the default character set.DATE: generates aDATEvalue.DATETIME [ (M) ]: generates aDATETIMEvalue. TheMvalue is optional and specifies the precision of seconds in the decimal part, 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.Mcan be up to 65, andDcan be up to 30. IfDis omitted, it defaults to0; ifMis omitted, it defaults to10.SIGNED [INTEGER]: generates a signedBIGINTvalue.JSON: generates aJSONvalue. For more information about the conversion rules betweenJSONand other data types, see JSON data type conversion.TIME [ (M) ]: generates aTIMEvalue. If an optionalMvalue is specified, it specifies the precision of seconds in the decimal part.UNSIGNED [INTEGER]: generates an unsignedBIGINTvalue.DECIMAL(m,d): a fixed-point number type, wheremis the total number of digits anddis the number of digits after the decimal point.FLOAT: a single-precision floating-point number, suitable for approximate numeric values.INTorINTEGER: a 32-bit signed integer.SMALLINT: a 16-bit signed integer.TINYINT: an 8-bit signed integer.ARRAY: converts elements of aJSONarray to a specific type of array.
Considerations
When using the CAST function for data type conversion, the following cases 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 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 theJSONtype 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 be returned during conversion.
If an impossible conversion is attempted, OceanBase Database will display an error message. If the length of the data type is not specified during conversion, the maximum length supported by the OceanBase Database system will be used. For example, VARCHAR can be up to 262,143 bytes, and NUMBER can be 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 (such as +) 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 numeric value 0 to the
DATEtype.obclient> SELECT CAST(0 AS DATE); +-----------------+ | CAST(0 AS DATE) | +-----------------+ | 0000-00-00 | +-----------------+ 1 row in setConvert the numeric value 123 to the
TIMEtype.obclient> SELECT CAST(123 AS TIME); +-------------------+ | CAST(123 AS TIME) | +-------------------+ | 00:01:23 | +-------------------+ 1 row in setConvert the numeric 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 numeric 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
JSONtype.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.
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
CAST().obclient> SELECT CAST(1 AS UNSIGNED) - 2.0; +---------------------------+ | CAST(1 AS UNSIGNED) - 2.0 | +---------------------------+ | -1.0 | +---------------------------+ 1 row in setConvert the numeric value 123 to the
CHARtype.obclient> SELECT CAST(123 AS CHAR(2)); +----------------------+ | CAST(123 AS CHAR(2)) | +----------------------+ | 12 | +----------------------+ 1 row in setConvert the numeric 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 string
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. The statement is as follows:
obclient> SELECT CAST( JSON '[1,2,3]' AS ARRAY<int>);
+-------------------------------------+
| CAST( JSON '[1,2,3]' AS ARRAY<int> |
+-------------------------------------+
| [1,2,3] |
+-------------------------------------+
1 row in set (0.035 sec