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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM