CAST 函数
在之前的文章中,我们提到过CAST函数,用于显示进行类型转换。在避免隐式类型转换时,好处多多。其实这里面还有很多细节,是需要梳理下的。
首先来看看下面这个转换:
mysql> SELECT CAST('2017-12-14' AS DATE); +----------------------------+ | CAST('2017-12-14' AS DATE) | +----------------------------+ | 2017-12-14 | +----------------------------+ 1 row in set (0.00 sec)
其中:
2017-12-14 是待转换的数据。
DATE为转换后的类型。
标准语法是这样的:
CAST(expr AS type)
这里需要注意的是type类型不支持所有的数据类型,而是支持特定的数据类型,也是今天这篇文章的重点。(我就吃过这个亏,想当然的以为支持所有数据类型,结果就被打脸了) 。
不支持的报错:
mysql> SELECT CAST('1024' AS int); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int)' at line 1
支持的类型
下面是CAST函数支持转换的数据类型列表:
类型 | 备注 |
---|---|
DATE | YYYY-MM-DD |
DATETIME | YYYY-MM-DD HH:mm:ss |
TIME | HH:mm:ss |
DECIMAL | 通常用于带小数位 |
CHAR | 固定长度字符串 |
NCHAR | 类型于CHAR一致 |
SIGNED | 一个有符号的64整数位 |
UNSIGNED | 一个无符号的64整数位 |
BINARY | 二进制字符串 |
JSON | MySQL 5.7.8 及更高版本 |
注意:
其中DATE支持的范围是: 1000-01-01 至 9999-12-31,(实验版本为:)
如果为: 999-01-01 结果则会0999-01-01。
如果为: 01-01-01则会为:2001-01-01。
mysql> select cast('999-11-11' as DATE); +---------------------------+ | cast('999-11-11' as DATE) | +---------------------------+ | 0999-11-11 | +---------------------------+ 1 row in set (0.00 sec)
mysql> select cast('01-11-11' as DATE); +--------------------------+ | cast('01-11-11' as DATE) | +--------------------------+ | 2001-11-11 | +--------------------------+ 1 row in set (0.00 sec)
mysql> select version(); +-----------+ | version() | +-----------+ | 5.7.20 | +-----------+ 1 row in set (0.00 sec)
2. CAST函数中expr的值可以转换为type,其转换的结果才正确,否则转换后的结果默认值,如Null,0等。
例如:一个Char类型转换为Demical类型,转换的结果就为0。
mysql> SELECT CAST('ANDYQIAN' AS DECIMAL); +-----------------------------+ | CAST('ANDYQIAN' AS DECIMAL) | +-----------------------------+ | 0 | +-----------------------------+ 1 row in set, 1 warning (0.00 sec)
转换案例
这里给出一些常用的类型转换例子。
DATE 类型
mysql> select cast('2017-12-14' as DATE); +----------------------------+ | cast('2017-12-14' as DATE) | +----------------------------+ | 2017-12-14 | +----------------------------+ 1 row in set (0.00 sec)
TIME 类型
mysql> select cast('12:00:00' as TIME); +--------------------------+ | cast('12:00:00' as TIME) | +--------------------------+ | 12:00:00 | +--------------------------+ 1 row in set (0.00 sec)
DATETIM 类型
mysql> select cast('2017-12-14 00:11:11' as DATETIME); +-----------------------------------------+ | cast('2017-12-14 00:11:11' as DATETIME) | +-----------------------------------------+ | 2017-12-14 00:11:11 | +-----------------------------------------+ 1 row in set (0.00 sec)
SIGNED类型
mysql> select cast('-1024' as SIGNED); +-------------------------+ | cast('-1024' as SIGNED) | +-------------------------+ | -1024 | +-------------------------+ 1 row in set (0.00 sec)
UNSIGNED类型
mysql> select cast('-1024' as UNSIGNED); +---------------------------+ | cast('-1024' as UNSIGNED) | +---------------------------+ | 18446744073709550592 | +---------------------------+ 1 row in set, 1 warning (0.00 sec)
DECIMAL类型
mysql> select cast('18.11' as DECIMAL(18,2)); +--------------------------------+ | cast('18.11' as DECIMAL(18,2)) | +--------------------------------+ | 18.11 | +--------------------------------+ 1 row in set (0.00 sec)