Question : mysql: convert 2 bytes from blob to integer

Hi,

I have a BLOB field and i want to take 2 bytes from it and use them as an integer.  How can i do this?  

EG:
select substr(blobfield,195,2) as value1 from mytable;

the 2 bytes on position 195 and 196 are an integer that was stored inside a blob field.

How can i get the integer back in mysql, so i can perform a where / having on this value ?






Answer : mysql: convert 2 bytes from blob to integer

Depending on what you exactly have in the BLOB you could try either to get the ASCII values of each byte and calculate the integer:
---
SELECT ascii(substr(blobfield, 2, 1)) * 256 + ascii(substr(blobfield, 3, 1)) as myNumber FROM myBlob;
---

Or if you have number characters, then you could try:
---
SELECT (ascii(substr(blobfield, 2, 1)) - 48) * 10 + (ascii(substr(blobfield, 3, 1)) - 48) as myNumber FROM myBlob;
---

Hope it helps.
Random Solutions  
 
programming4us programming4us