mysqlcast和convert函数学习(将字符串转数字)
[size=medium][color=red][b]MySQL 的CAST()和CONVERT()函数可⽤来获取⼀个类型的值,并产⽣另⼀个类型的值。两者具体的语法如下:[/b][/color][/size]
CAST(value as type);
CONVERT(value, type);
就是CAST(xxx AS 类型), CONVERT(xxx,类型)。
可以转换的类型是有限制的。这个类型可以是以下值其中的⼀个:
⼆进制,同带binary前缀的效果 : BINARY
字符型,可带参数 : CHAR()
⽇期 : DATE
transform和convert的区别[b][color=red]时间: TIME[/color][/b]
⽇期时间型 : DATETIME
浮点数 : DECIMAL
[color=red][b]整数 : SIGNED [/b][/color]
⽆符号整数 : UNSIGNED
下⾯举⼏个例⼦:
mysql> SELECT CONVERT('23',SIGNED);
+----------------------+
| CONVERT('23',SIGNED) |
+----------------------+
| 23 |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT CAST('125.34edf342.83' AS SIGNED);
+-----------------------------------+
| CAST('125.34edf342.83' AS SIGNED) |
+-----------------------------------+
| 125 |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT CAST('3.35' AS SIGNED);
+------------------------+
| CAST('3.35' AS SIGNED) |
+------------------------+
| 3 |
+------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT CAST('45' AS SIGNED);
+----------------------+
| CAST('45' AS SIGNED) |
+----------------------+
| 45 |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT CAST('-45' AS SIGNED);
+-----------------------+
| CAST('-45' AS SIGNED) | +-----------------------+
| -45 |
+-----------------------+
1 row in set (0.00 sec)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论