I have a binary code of 35 characters and have the expected output. Need the logic on how to convert that in SQL Server.
Input (binary): 00000110010110110001110101101110100
Expected output (int): 816570
Thanks in advance.
I have a binary code of 35 characters and have the expected output. Need the logic on how to convert that in SQL Server.
Input (binary): 00000110010110110001110101101110100
Expected output (int): 816570
Thanks in advance.
Try below query using recursive CTE:
declare @bit varchar(50) = '00000110010110110001110101101110100'
;with cte as (
select 0 [bit], cast(1 as bigint) powerOf2, substring(@bit, len(@bit) - 1, 1) [bitValue]
union all
select [bit] + 1, powerOf2 * 2, substring(@bit, len(@bit) - [bit] - 1, 1)
from cte
where [bit] + 1 < len(@bit)
)
select sum(powerOf2 * bitValue) from cte
It's important to make second column as bigint
, so it can hold big integer values :)