如何把用SQL语句int型整数转换成二进制数
用函数
------------------------
Create FUNCTION DecToBin(@Dec Int)
RETURNS VARCHAR(16)
AS
begin
declare @BinStr as varchar(20)
declare @Mod2 as int
declare @Div2 as int
set @Div2 = @Dec / 2
set @Mod2 = @Dec % 2
set @BinStr=''
while @Div2<>0
begin
if @Mod2=0
set @BinStr = @BinStr + '0'
else
set @BinStr = @BinStr + '1'
set @Dec = @Dec / 2
set @Div2 = @Dec / 2
set @Mod2 = @Dec % 2
end
set @BinStr = @BinStr + '1'
return REVERSE(@BinStr + right('0000000000000000',16-len(@BinStr)))
end