MySQL's cast () and CONVERT () functions can be used to get a value of one type and produce a value of another type. The specific syntax for both is as follows:
is cast (xxx as type), CONVERT (XXX, type).
There is a limit to the types that can be converted. This type can be one of the following values:
- Binary, with binary prefix effect: binary
- Character type, with parameters: CHAR () What are the casinos in Macau?
- Date: Date
- Time:
- DateTime Date/Time type
- Floating point number: DECIMAL
- Integer: Signed
- unsigned integer: UNSIGNED
Here are a few examples:
Example One
1 |
mysql> SELECT CONVERT ( ‘23‘ ,SIGNED); |
2 |
+ ----------------------+ |
3 |
| CONVERT ( ‘23‘ ,SIGNED) | |
4 |
+ ----------------------+ |
6 |
+ ----------------------+ |
Example Two
1 |
mysql> SELECT CAST ( ‘125e342.83‘ AS signed); |
2 |
+ ------------------------------+ |
3 |
| CAST ( ‘125e342.83‘ AS signed) | |
4 |
+ ------------------------------+ |
6 |
+ ------------------------------+ |
Example Three
1 |
mysql> SELECT CAST ( ‘3.35‘ AS signed); |
2 |
+ ------------------------+ |
3 |
| CAST ( ‘3.35‘ AS signed) | |
4 |
+ ------------------------+ |
6 |
+ ------------------------+ |
As in the example above, the varchar is converted to int with cast (a as signed), where a is a varchar-type string.
Example 4
In SQL Server, the following code demonstrates the result of a DateTime variable that contains only a single date and a simple time when the date stores the hexadecimal store representation.
05 |
SELECT CAST(@dt as binary(8)) |
06 |
--结果: 0x0000000100000000 |
10 |
SELECT CAST(@dt as binary(8)) |
11 |
--结果: 0x000000000000012C |
MySQL type conversion is like SQL Server, that is, the type parameter is a little different: CAST (xxx as type), convert (XXX, type).
The use of the MySQL cast and CONVERT functions