l0b0
l0b0

Reputation: 58848

How to get do binary operations on the Nth byte in my varbinary column?

I've been able to get the Nth byte out of the field with substring(colname, N, 1), but at that point it seems impossible to treat the result as binary:

> select substring(colname, N, 1) from [...]
\
> select hex(substring(colname, N, 1)) from [...]
5C
> select hex(substring(colname, N, 1) & 0xff) from [...]
0
> select cast(substring(colname, N, 1) as unsigned integer) from [...]
0

Compared to:

> select cast(0x5c as binary);
\
> select hex(0x5c & 0xff);
5C
> select cast(0x5c as unsigned integer);
92

What I want to end up with is something like this:

> select [...] where substring(colname, N, 1) & 0b00100000 = 0b00100000;

Upvotes: 0

Views: 243

Answers (1)

Vasyl Moskalov
Vasyl Moskalov

Reputation: 4630

Try something like that:

select ascii(substring(colname,N,1)) from [...]

Upvotes: 1

Related Questions