Binary To Decimal In Microsoft SQL
I wrote this one to help a guy out that had posted on www.houseoffusion.com. I think it is an interesting approach to solve the problem of converting a binary octet to decimal, so I decided to post it here for posterity's sake.
SQL:
-
DECLARE
-
@myOctet varchar(32),
-
@i int,
-
@power int,
-
@result int
-
SET @myOctet = reverse('00111100')
-
SET @power = len(@myOctet) - 1
-
SET @i = @power + 1
-
SET @result = 0
-
WHILE @i>= 1
-
BEGIN
-
SET @result = @result + substring(@myOctet,@i,1) * Power(2,@power)
-
SET @i = @i - 1
-
SET @power = @power - 1
-
END
-
SELECT @result

