Working with Binary Data in MySQL

我们两清 提交于 2020-01-04 11:43:54

问题


SELECT 
      0x0000987C As col1,
      substr(BinaryData,1,4) As col2,
      CAST(0x0000987C  AS SIGNED) As col3,
      CAST(substr(BinaryData,1,4)  AS SIGNED) As col4
FROM
(
SELECT 0x0000987C00000000 AS BinaryData
) d

Returns

col1  col2   col3  col4
----  ----  -----  ----
BLOB  BLOB  39036   0

When I look at the BLOB viewer for col1 and col2 they both appear identical (screenshot below).

So why the different results for col3 and col4?


回答1:


I think it has to do with data types. BinaryData has an integer data type, but substr(BinaryData,1,4) expects a string. CAST then gets confused with the result. Also, CAST parses strings using base 10, so you need to a little bit of extra work. Try this:

CAST(CONV(substr(HEX(BinaryData),1,8), 16, 10)  AS SIGNED) As col4

It's a monster, but it should give you what you want.



来源:https://stackoverflow.com/questions/4948062/working-with-binary-data-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!