The CAST () and CONVERT () functions of MySQL can be used to obtain values of one type and generate values of another type. The syntax is as follows: CAST (valueastype); CONVERT (value, type); CAST (xxxAS type), CONVERT (xxx, type ). There are limits on the types that can be converted. This type can be one of the following values:
The CAST () and CONVERT () functions of MySQL can be used to obtain values of one type and generate values of another type. The syntax is as follows: CAST (value AS type); CONVERT (value, type); CAST (xxx as type), CONVERT (xxx, type ). There are limits on the types that can be converted. This type can be one of the following values:
The CAST () and CONVERT () functions of MySQL can be used to obtain values of one type and generate values of another type. The specific syntax of the two is as follows:
CAST(value as type);CONVERT(value, type);
Is CAST (xxx AS type), CONVERT (xxx, type ).
There are limits on the types that can be converted. This type can be one of the following values:
- Binary, which has the same effect as BINARY? ?
- Character type, which can contain parameters: CHAR ()? ??
- DATE: DATE? ??
- TIME: TIME? ??
- DATETIME type: DATETIME? ??
- Floating Point: DECIMAL? ? ?
- Integer: SIGNED? ??
- UNSIGNED integer: UNSIGNED?
The following are examples:
Example 1
mysql> SELECT CONVERT('23',SIGNED);+----------------------+| CONVERT('23',SIGNED) |+----------------------+| 23 |+----------------------+1 row in set
Example 2
mysql> SELECT CAST('125e342.83' AS signed);+------------------------------+| CAST('125e342.83' AS signed) |+------------------------------+| 125 |+------------------------------+1 row in set
Example 3
mysql> SELECT CAST('3.35' AS signed);+------------------------+| CAST('3.35' AS signed) |+------------------------+| 3 |+------------------------+1 row in set
As in the preceding example, convert varchar to int using cast (a as signed), where a is a string of the varchar type.
Example 4
In SQL Server, the following code demonstrates that the hexadecimal storage of the Date Storage represents only the date and time when the datetime variable contains the pure date.
DECLARE @ dt datetime -- simple date SET @ dt = '2017-1-2 'select cast (@ dt as binary (8) -- result: 0x0000000100000000 -- simple time SET @ dt = '00: 00: 01 'select cast (@ dt as binary (8) -- result: 0x0000000000000000012c
MySQL type conversion is similar to SQL Server, that is, the type parameters are a little different: CAST (xxx AS type), CONVERT (xxx, type ).