Simon
Simon

Reputation: 509

get partial data from BLOB as hex

I have a long binary written to MySQL as long BLOB. When I read the BLOB back from the DB by my PHP app, I would like to take the first 12 bytes only as hex. I successfully converted the BLOB to hex by using bin2hex(BLOB) but don't know how to ignore the data over the first 12 bytes.

Upvotes: 0

Views: 4702

Answers (1)

peterm
peterm

Reputation: 92805

Are you looking for something this?

SELECT HEX(LEFT(data, 12)) partial
  FROM table1

Here is SQLFiddle demo

The BLOB and TEXT Types

BLOB values are treated as binary strings (byte strings).

Therefore you can grab first 12 bytes with LEFT() and then convert them with HEX()


I believe in Codeigniter you can do something like this

$result = $this->db
               ->select('HEX(LEFT(data, 12)) AS partial', FALSE)
               ->from('table1')
               ->get();

Upvotes: 4

Related Questions