Syntax
CAST(expr AS type)
Purpose
You can call the CAST() function to explicitly convert the expression of a data type to another data type, that is, convert the value specified by expr to a value of the data type specified by type.
Parameters
The parameters are described as follows:
expr: specifies a valid SQL expression.AS: separates two arguments. The value before AS specifies the data to be processed, and the value after AS specifies the target data type.type: specifies the data type provided by the target system.Supported data types:CHAR[(N)] [CHARACTER SET charset_name]Generates a string of the
CHARdata 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. Value range: [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. If M is specified, it indicates the precision of seconds, that is, the number of decimal places for seconds.UNSIGNED [INTEGER]: generates an unsignedBIGINTvalue.
Usage notes
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 value length is not specified for the target data type, the maximum length of the data type 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.
CAST() supports the calculation of signed and unsigned 64-bit values. If you are using a numeric operator (for example, +) and one of the operands is an unsigned integer, the result is unsigned. You can use SIGNED and UNSIGNED to explicitly declare the result and assign the operation to 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