Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
create function fn_ConvertBin
@EnteredData = varchar(8)
as
returns varchar(25)
declare @ReturnData varchar(25)
set @ReturnData = ''
if SubString(@EnteredData, 8,1) = 1
set @ReturnData = @ReturnData + '1,'
if SubString(@EnteredData,7,1) = 1
set @ReturnData = @ReturnData + '2,'
if SubString(@EnteredData,6,1) = 1
set @ReturnData = @ReturnData + '4,'
if SubString(@EnteredData,5,1) = 1
set @ReturnData = @ReturnData + '5,'
...
If @ReturnData <> '' /*Trim the last comma as needed*/
set @ReturnData = left(@ReturnData, len(@ReturnData)-1)
return @ReturnData
BTW 44 = 00101100
00101010 = 42
create function fn_ConvertBin
(@EnteredData varchar(8))
returns varchar(25)
as
Begin
declare @ReturnData varchar(25)
set @ReturnData = ''
if SubString(@EnteredData, 8,1) = 1
set @ReturnData = @ReturnData + '1,'
if SubString(@EnteredData, 8,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData,7,1) = 1
set @ReturnData = @ReturnData + '2,'
if SubString(@EnteredData,7,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData,6,1) = 1
set @ReturnData = @ReturnData + '4,'
if SubString(@EnteredData,6,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData,5,1) = 1
set @ReturnData = @ReturnData + '8,'
if SubString(@EnteredData,5,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData, 4,1) = 1
set @ReturnData = @ReturnData + '16,'
if SubString(@EnteredData, 4,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData,3,1) = 1
set @ReturnData = @ReturnData + '32,'
if SubString(@EnteredData,3,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData,2,1) = 1
set @ReturnData = @ReturnData + '64,'
if SubString(@EnteredData,2,1) = 0
set @ReturnData = @ReturnData + '0,'
if SubString(@EnteredData,1,1) = 1
set @ReturnData = @ReturnData + '128,'
if SubString(@EnteredData,1,1) = 0
set @ReturnData = @ReturnData + '0,'
If @ReturnData <> '' /*Trim the last comma as needed
- comma needed in this case*/
set @ReturnData = @ReturnData
Return @ReturnData
End
CREATE PROC BIN_TO_NUMBER (@BIN_INPUT varchar (8))
AS
DECLARE @s varchar(100)
DECLARE @table table (ID varchar(20))
SET @s = (select dbo.fn_ConvertBin(@BIN_INPUT))
WHILE (@s != '')
BEGIN
INSERT @table
SELECT SUBSTRING(@s,1,PATINDEX('%,%',@s) - 1)
SELECT @s = RIGHT(@s,LEN(@s) - PATINDEX('%,%',@s))
END
SELECT sum(cast(ID as int)) FROM @table
EXEC BIN_TO_NUMBER '11111111'