Syntax
CAST(expr AS type)
Purpose
CAST() explicitly converts the expression of a data type to another data type. That is, it converts the value specified by expr to a value of the data type specified by type.
Parameters
The parameters are described as follows:
expr: a valid SQL expression.AS: separates two parameters. The parameter beforeASspecifies the data to be processed, and the parameter afterASspecifies the target data type.type: the data type supported in the target system. The supported data types are as follows:CHAR[(N)] [CHARACTER SET charset_name]: generates a string of theCHARdata type. The data type of the result isCHAR(0)unlessexpris null (length: 0). IfNis specified,CHAR(N)will be forcibly converted to a string with no more thanNarguments. No padding is performed for values with less thanNcharacters. IfNis not specified, OceanBase Database calculates the maximum length based on the expression. Without theCHARACTER SET charset_nameclause,CHARgenerates a string of the default character set.DATE: generates aDATEvalue.DATETIME [ (M) ]: generates aDATETIMEvalue.Mis optional and specifies the precision of seconds in the decimal place. The value range is [0,6].DECIMAL [ (M [,D] ) ]: generates aDECIMALvalue.MandDare optional and respectively specify the maximum number of digits (precision) and the number of decimal digits. The maximum value ofMis 65, and that ofDis 30. IfDis omitted, the default value is0. IfMis omitted, the default value is10.SIGNED [INTEGER]: generates a signedBIGINTvalue.JSON: generates aJSONvalue. For more information about the conversion rules between JSON and other data types, see Convert a JSON data type.TIME [ (M) ]: generates aTIMEvalue. IfMis specified, it indicates the precision of seconds, that is, the number of decimal places for seconds.UNSIGNED [INTEGER]: generates an unsignedBIGINTvalue.
Considerations
You can use the CAST function to convert data types in the following scenarios:
The data types of the two expressions are the same.
The two expressions can be implicitly converted.
The data type must be explicitly converted.
If you attempt to make an unacceptable conversion, OceanBase Database returns an error message. If the data type length is not specified, the maximum length in the OceanBase Database system is used. For example, the maximum length of a VARCHAR value is 262,143 bytes, and that of a NUMBER value has a floating precision of 65 bits.
The CAST() function supports the operation on 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 is unsigned. You can use SIGNED or UNSIGNED to explicitly specify the result. This way, the operation is specified as signed or unsigned 64-bit integers. If an operand is a floating-point value, the result is a floating-point value.
Examples
Convert 0 to a
DATEvalue.obclient> SELECT CAST(0 AS DATE); +-----------------+ | CAST(0 AS DATE) | +-----------------+ | 0000-00-00 | +-----------------+ 1 row in setConvert 123 to a
TIMEvalue.obclient> SELECT CAST(123 AS TIME); +-------------------+ | CAST(123 AS TIME) | +-------------------+ | 00:01:23 | +-------------------+ 1 row in setConvert 123 to a
DATETIMEvalue.obclient> SELECT CAST(123 AS DATETIME(4)); +--------------------------+ | CAST(123 AS DATETIME(4)) | +--------------------------+ | 2000-01-23 00:00:00.0000 | +--------------------------+ 1 row in setConvert 123 to a
DECIMALvalue.obclient> SELECT CAST(123 AS DECIMAL(3,2)); +---------------------------+ | CAST(123 AS DECIMAL(3,2)) | +---------------------------+ | 9.99 | +---------------------------+ 1 row in setConvert the string literal "123" to a JSON value.
obclient> SELECT CAST("123" AS JSON); +---------------------+ | CAST("123" AS JSON) | +---------------------+ | 123 | +---------------------+ 1 row in setOutput the result of "1-2" separately as a signed value and an unsigned value.
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 setUse
CAST()for numeric calculation.obclient> SELECT CAST(1 AS UNSIGNED) - 2.0; +---------------------------+ | CAST(1 AS UNSIGNED) - 2.0 | +---------------------------+ | -1.0 | +---------------------------+ 1 row in setConvert 123 to a
CHARvalue.obclient> SELECT CAST(123 AS CHAR(2)); +----------------------+ | CAST(123 AS CHAR(2)) | +----------------------+ | 12 | +----------------------+ 1 row in setConvert 1 to a
CHARvalue and specify a character set.obclient> SELECT CAST(1 AS CHAR CHARACTER SET utf8mb4); +---------------------------------------+ | CAST(1 AS CHAR CHARACTER SET utf8mb4) | +---------------------------------------+ | 1 | +---------------------------------------+ 1 row in set