1、将Int 转为varchar经常用 concat函数,比如concat(8,’0′) 得到字符串 ’80′。
2、将varchar 转为Int 用 cast(a as signed) a为varchar类型的字符串。
总结:类型转换和SQL Server一样,就是类型参数有点点不同 : CAST(xxx AS 类型) , CONVERT(xxx,类型)。
-- 比如将123转换为char类型
SELECT CAST(123 AS CHAR)
数字转字符mysql> SELECT CONCAT ( CAST(1 as char) , '2') AS test
+------+
| test |
+------+
| 12 |
+------+
1 row in set (0.00 sec)
mysql> SELECT CONCAT ( Convert(1, char) , '2') AS test
+------+
| test |
+------+
| 12 |
+------+
1 row in set (0.00 sec)
字符转数字
mysql> SELECT CAST('1' as SIGNED) + 100 AS test
+------+
| test |
+------+
| 101 |
+------+
1 row in set (0.00 sec)
mysql> SELECT Convert('1' , SIGNED) + 100 AS test
+------+
| test |
+------+
| 101 |
+------+
1 row in set (0.00 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)