【问题标题】:MariaDB cast binary as intMariaDB 将二进制转换为 int
【发布时间】:2015-05-17 03:03:15
【问题描述】:

美好的一天!
我有一个带有二进制列 C1 的表 T1(在 MariaDB)。它有一些字节。

当我选择 HEX 时,我有以下内容:

select HEX(SUBSTRING((select C1 from T1 where <some restrictions>), 254, 2));
+----------------------------------------------------------------------+
| HEX(SUBSTRING((select C1 from T1 where <some restrictions>), 254, 2)) |
+----------------------------------------------------------------------+
| 1200                                                                 |
+----------------------------------------------------------------------+

但我未能将结果转换为 INT:

select cast((SUBSTRING((select C1 from T1 where <some restrictions>), 254, 2)) as int);
+--------------------------------------------------------------------------------+
| cast((SUBSTRING((select C1 from T1 where <some restrictions>), 254, 2)) as int) |
+--------------------------------------------------------------------------------+
|                                                                              0 |
+--------------------------------------------------------------------------------+

所以,我的问题是 - 为什么会发生以及如何将二进制转换为 int?

【问题讨论】:

  • UNHEX("1200") 不是一串数字,因此被解释为 0。(substring 是红鲱鱼。)

标签: mysql binary mariadb


【解决方案1】:

类型为:无符号整数有符号整数

select cast((substring((select C1 from T1 where <some restrictions>), 254, 2)) as unsigned integer);

select cast(substring( '12345', 1, 2 ) as unsigned integer) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    相关资源
    最近更新 更多