UFN_CONVERT_INT_TO_BINARY

Return

Return Type
nvarchar(1000)

Parameters

Parameter Parameter Type Mode Description
@VALUE int IN
@FIXEDSIZE int IN

Definition

Copy


        create function dbo.UFN_CONVERT_INT_TO_BINARY
        (
         @VALUE INT,
         @FIXEDSIZE INT
        )
        returns nvarchar(1000)
        as
        begin
         if @FIXEDSIZE is null
            set @FIXEDSIZE = 10;
         declare @RESULT nvarchar(1000) = '';

         while (@VALUE != 0)
         begin
          if(@VALUE%2 = 0
           set @RESULT = '0' + @RESULT;
          else
           set @RESULT = '1' + @RESULT;

          set @VALUE = @VALUE / 2;
         end;

        if(@FIXEDSIZE is not null and @FIXEDSIZE > 0 and len(@RESULT) < @FIXEDSIZE)
          select @RESULT = right(replicate(0, @FIXEDSIZE)+ @RESULT, @FIXEDSIZE)

         return @RESULT;
        end