Sunday, March 25, 2012

Binary Value Manipulation

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.

Here is simplified code:

select right(0x88186000,8)

I expected to get back 88186000, this was not the case. The command
returned some wierd characters.

Am I missing something?tfeller <ToddFeller@.gmail.comwrote in news:1178044017.983887.201130
@.o5g2000hsb.googlegroups.com:

Quote:

Originally Posted by

I would like to drop the leading 0x on a binary value so I can do a
bitwise operation.
>
Here is simplified code:
>
select right(0x88186000,8)
>
I expected to get back 88186000, this was not the case. The command
returned some wierd characters.
>
Am I missing something?
>


What do you get back from SELECT 0x88186000 ?

I don't expect it will be 0x88186000

Try SELECT CAST(0x41424344 as varchar) and you should get back ABCD

The _representation_ (note emphasis) of a binary value _in T-SQL_ is a
sequence of characters that begin with 0x - but the 0x does not form part
of the _actual binary value_, any more than the starting and ending quotes
form part of a character value.|||The expressions in the bitwise operations are treated as binary numbers, and
one of the expressions can be a binary data type. So, you do not have to
convert your binary value, just use it directly. The results is of data type
integer and you can convert it back to binary. Try this:

SELECT CAST(0x88186000 ^ 2 AS binary(4)),
CAST(0x88186000 | 2 AS binary(4)),
CAST(0x88186000 & 2 AS binary(4))

HTH,

Plamen Ratchev
http://www.SQLStudio.com|||On 1 May 2007 11:26:58 -0700, tfeller wrote:

Quote:

Originally Posted by

>I would like to drop the leading 0x on a binary value so I can do a
>bitwise operation.
>
>Here is simplified code:
>
>select right(0x88186000,8)
>
>I expected to get back 88186000, this was not the case. The command
>returned some wierd characters.
>
>Am I missing something?


Hi tfeller,

Maybe the functions Peter DeBetta describes will help you to achieve
what you need:
http://sqlblog.com/blogs/peter_debe...-varbinary.aspx
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelissql

No comments:

Post a Comment