• Some one suggested this function works

    ALTER function [dbo].[ConvertBigIntToVarBinary1](@param bigint)

    returns varbinary(1000) AS

    begin

    declare @iPower int;

    declare @result varbinary(1000);

    declare @powerOf2 bigint;

    set @result = 0x;

    set @powerOf2 = 1;

    set @iPower = 0;

    while @powerOf2 <= @param

    begin

    set @result = @result + CAST(@param / @powerOf2 % 2 AS binary(1));

    set @iPower = @iPower + 1;

    set @powerOf2 = power(2., @iPower);

    end

    return @result

    end

    Although I dont see much advance of it plus it fails to convert the max bigint value for some reasons

    SELECT dbo.ConvertBigIntToVarBinary1(9223372036854775807)