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 2008 Forums
 Transact-SQL (2008)
 Problem with BINARY

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2011-04-07 : 02:22:15
Hi,
My purpose is to splitting bits of a binary value. Means showing values in bit pattern then splitting them.
For example:

value(decimal)    first_bit      second_bit        third_bit
------------------------------------------------------------
0 0 0 0
1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
...
...
7 1 1 1




______________________

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-04-07 : 03:28:41
Post some data how you want to split into binary. With examples


Raghu' S
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-04-07 : 03:41:22
[code]
select value,
bit_4 = case when value& 8 = 0 then 0 else 1 end,
bit_3 = case when value& 4 = 0 then 0 else 1 end,
bit_2 = case when value& 2 = 0 then 0 else 1 end,
bit_1 = case when value& 1 = 0 then 0 else 1 end
from table
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2011-04-07 : 03:48:49
quote:
Originally posted by khtan


select value,
bit_4 = case when value& 8 = 0 then 0 else 1 end,
bit_3 = case when value& 4 = 0 then 0 else 1 end,
bit_2 = case when value& 2 = 0 then 0 else 1 end,
bit_1 = case when value& 1 = 0 then 0 else 1 end
from table



KH
[spoiler]Time is always against us[/spoiler]





Thank you so much!
You post exactly my needed.

select	value,
--bit_4 = case when value & 8 = 0 then 0 else 1 end,
bit_3 = case when value & 4 = 0 then 0 else 1 end,
bit_2 = case when value & 2 = 0 then 0 else 1 end,
bit_1 = case when value & 1 = 0 then 0 else 1 end
from (select 0 union
select 1 union
select 2 union
select 3 union
select 4 union
select 5 union
select 6 union
select 7)d(value);
/*
value bit_3 bit_2 bit_1
----------- ----------- ----------- -----------
0 0 0 0
1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1
*/


______________________
Go to Top of Page
   

- Advertisement -