SQL Server 二进制转为十进制

复制代码
CREATE FUNCTION BinaryToDec (@BinaryChar char(10))  
RETURNS int 
AS  
BEGIN 
    DECLARE @stringLength int,@ReturnValue int,@Index int
    DECLARE @CurrentChar char(1)
    SET @Index = 0
    SET @ReturnValue = 0
    SET @stringLength = LEN(@BinaryChar)
    While @Index<@stringLength
        BEGIN
            SET @Index = @Index + 1
            SET @CurrentChar = SUBSTRING(@BinaryChar,@Index,1)
            IF(@CurrentChar='1' or @CurrentChar='0')
                BEGIN
                    SET @ReturnValue = @ReturnValue + (CAST(@CurrentChar as int) * POWER(2,@stringLength - @Index))
                END
        END
    RETURN @ReturnValue
END
复制代码

 

posted @   黑冰.org  阅读(4084)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示