Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Binary Field

Author  Topic 

Karander
Starting Member

34 Posts

Posted - 2006-12-27 : 08:29:27
Hi,
How to set exactly n-th bit in binary field?

For Instance:
0x0000 0000 0000 0010

And i want to set '1' on 2. bit
in result i would like to have:
0x0000 0000 0000 0010

and ...
0x0000 0000 0000 0010
to
0x0000 0000 0000 0000

Thx fo help



harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-27 : 08:41:24
AND it with reverse bitmap:

(0x0000 0000 0000 0010 AND 0x0000 0000 0000 0010) AND (0x1111 1111 1111 1101)

In SQL Server,

Select (2 & 2) & 1


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Karander
Starting Member

34 Posts

Posted - 2006-12-27 : 08:58:24
but if i had to create function setting appropiate bit in value and returning binary field, how would you do it? :)

A udf function passing binary value, bit to set and position...
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-27 : 09:33:29
There is an undocumented SP - sp_MSsetbit. See if it helps.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -