您的位置:首页 > 数据库

SQL Server 二进制转为十进制

2012-08-31 12:04 218 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: