无法转换变量MYSQL

时间:2015-02-22 06:20:42

标签: mysql casting

我正在使用CAST()函数来转换已在会话期间设置的变量。下面的代码解释了它。

SET @X="'b'1000001''"

SET @Y=CAST(b'1000001 AS UNSIGNED);

(这完美的工作让我获得价值65)

SELECT @X;
(THIS GIVES ME THE VALUE OF X AS b'1000001')

 SET @Y=CAST(@X AS UNSIGNED);
 (THIS DOESNOT WORK!!)
 (GIVES ME VALUE AS 0 FOR Y)

任何人都可以向我解释为什么变量无法在cast()函数中转换变量@X。如果我想在mysql中转换变量,有什么工作吗?

1 个答案:

答案 0 :(得分:0)

mysql> SELECT CAST("'b'1000001''" AS UNSIGNED), CAST(b'1000001' AS UNSIGNED);
+----------------------------------+------------------------------+
| CAST("'b'1000001''" AS UNSIGNED) | CAST(b'1000001' AS UNSIGNED) |
+----------------------------------+------------------------------+
|                                0 |                           65 |
+----------------------------------+------------------------------+

您可以将数字值CAST到UNSIGNED;除非有前导数字,否则转换字符串将为零。