CAST
Syntax
CAST(expr AS type)
Description
CAST() explicitly converts an expression of one data type to another data type. This function is used to convert the value of the expr field to the type data type.
Parameters
The parameters are described as follows:
-
expr: An SQL expression. -
AS: Separates the two parameters. The value beforeASis the data to be processed, and the value afterASis the target data type. -
type: The data type of the target system. Supported types are as follows:-
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 you specify an optional lengthN,CHAR(N)forces the conversion of a string of characters that does not exceedNcharacters. If the value is shorter thanN, it is not padded. If you do not specify the optional lengthN, seekdb calculates the maximum length based on the expression. If you do not specify theCHARACTER SET charset_nameclause, the default character set is used. -
DATE: Returns aDATEvalue. -
DATETIME [ (M) ]: Returns aDATETIMEvalue.Mis an optional parameter that specifies the precision of the seconds in the fractional part, with a range of [0,6]. -
DECIMAL [ (M [,D] ) ]: Returns aDECIMALvalue.MandDare optional parameters that specify the maximum number of digits (precision) and the number of digits after the decimal point (scale), respectively. The maximum value forMis 65, and the maximum value forDis 30. If you omitD, it defaults to0. If you omitM, it defaults to10. -
SIGNED [INTEGER]: Returns a signedBIGINTvalue. -
JSON: Generates aJSONvalue. For information on how to convert between the JSON data type and other data types, see Conversion rules for JSON data type. -
TIME [ (M) ]: Returns aTIMEvalue. If the optional parameterMis specified, it specifies the precision of the seconds in the fractional part. -
UNSIGNED [INTEGER]: Returns an unsignedBIGINTvalue. -
DECIMAL(m,d): A fixed-point number type.mspecifies the total number of digits, anddspecifies the number of digits after the decimal point. -
FLOAT: A single-precision floating-point number, suitable for approximate values. -
INTorINTEGER: A 32-bit signed integer. -
SMALLINT: A 16-bit signed integer. -
TINYINT: An 8-bit signed integer. -
ARRAY: Converts elements from aJSONarray into a specific type of array.
-
Usage
When using the CAST function for data type conversion, the following conditions are supported:
-
The data types of the two expressions are identical.
-
The two expressions can be implicitly converted.
-
You must explicitly convert the data types.
-
If the value of the
exprexpression exceeds the range of the target data type, the type conversion will returnnull. -
Converting a high-precision data type to a low-precision data type may result in 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) may result in loss of precision. -
If the
exprexpression is of theVARCHARtype and not a numeric value, and you attempt to convert it toINT/INTEGER,SMALLINT, orTINYINT, the result will be0. -
If the
exprexpression is of the JSON type and not a numeric value, and you attempt to convert it toINT/INTEGER,SMALLINT, orTINYINT, an error will be returned. -
If the
VARCHARdata orJSONdata does not conform to theARRAYformat, an error will occur during conversion.
When users attempt an impossible conversion, seekdb will display an error message. If the length of the data type is not specified during conversion, seekdb will use its internal maximum length. For example, VARCHAR is 262,143 bytes, and NUMBER is 65 bits with floating point precision.
The CAST() function supports operations with both signed and unsigned 64-bit values. When using a numeric operator such as + 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 specify whether the operation should be with signed or unsigned 64-bit integers. If any of the operands is a floating-point value, the result is a floating-point value.
Examples
-
Convert the numeric value 0 to the
DATEtype.SELECT CAST(0 AS DATE);
+-----------------+
| CAST(0 AS DATE) |
+-----------------+
| 0000-00-00 |
+-----------------+
1 row in set (0.001 sec) -
Convert the numeric value 123 to the
TIMEtype.SELECT CAST(123 AS TIME);
+-------------------+
| CAST(123 AS TIME) |
+-------------------+
| 00:01:23 |
+-------------------+
1 row in set -
Convert the numeric value 123 to the
DATETIMEtype.SELECT CAST(123 AS DATETIME(4));
+--------------------------+
| CAST(123 AS DATETIME(4)) |
+--------------------------+
| 2000-01-23 00:00:00.0000 |
+--------------------------+
1 row in set -
Convert the numeric value 123 to the
DECIMALtype.SELECT CAST(123 AS DECIMAL(3,2));
+---------------------------+
| CAST(123 AS DECIMAL(3,2)) |
+---------------------------+
| 9.99 |
+---------------------------+
1 row in set -
Convert the string "123" to the JSON type.
SELECT CAST("123" AS JSON);
+---------------------+
| CAST("123" AS JSON) |
+---------------------+
| 123 |
+---------------------+
1 row in set -
Convert the result of "1-2" separately using unsigned and signed representations.
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 set -
Perform numerical operations using
CAST().SELECT CAST(1 AS UNSIGNED) - 2.0;
+---------------------------+
| CAST(1 AS UNSIGNED) - 2.0 |
+---------------------------+
| -1.0 |
+---------------------------+
1 row in set -
Convert the numeric value 123 to the
CHARtype.SELECT CAST(123 AS CHAR(2));
+----------------------+
| CAST(123 AS CHAR(2)) |
+----------------------+
| 12 |
+----------------------+
1 row in set -
Convert the numeric value 1 to the
CHARtype and specify the character set.SELECT CAST(1 AS CHAR CHARACTER SET utf8mb4);
+---------------------------------------+
| CAST(1 AS CHAR CHARACTER SET utf8mb4) |
+---------------------------------------+
| 1 |
+---------------------------------------+
1 row in set -
Convert the character value
123to an integer and the integer value 1 to theINTtype.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 sec -
Convert the
JSONdata [1,2,3] to anARRAYdata type consisting ofINTtype. The statement is as follows:
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