CREATE FUNCTION [dbo].[2Transfer10] (@Code Nvarchar(100)) RETURNS Int
AS
BEGIN
DECLARE @ResultVar Int
DECLARE @iLen Int
DECLARE @I Int
DECLARE @J INT
DECLARE @Number Int
SET @iLen = LEN(@Code)
SET @I = @iLen-1
SET @J = 1
SET @ResultVar = 0
WHILE (@I >= 0) AND (@iLen >= @J)
BEGIN
SET @Number = SUBSTRING(@Code, @J, 1)
IF @Number = 1
Begin
SET @ResultVar = @ResultVar + POWER(2,@I)
END
SET @I = @I - 1
SET @J = @J + 1
END
RETURN @ResultVar
END
EX:
Select [dbo].[2Transfer10](1101) ==>13
沒有留言:
張貼留言