2008年1月29日 星期二

16進制轉成2進制

ALTER FUNCTION [dbo].[16Transfer2] (@Code Nvarchar(20))
RETURNS Nvarchar(100)
AS
BEGIN
 DECLARE @ResultVar Nvarchar(100)
 DECLARE @iLen Int
 DECLARE @I Int
 DECLARE @SingleCode Nvarchar(2)
 DECLARE @Ten INT

 SELECT @iLen = LEN(@Code)
 SET @I = 1
 SET @ResultVar = ''

 While @I <= @iLen
 BEGIN
  SET @SingleCode = SUBSTRING(@Code, @I, 1)

  SET @Ten = CASE @SingleCode
        WHEN 'A' THEN 10
        WHEN 'B' THEN 11
        WHEN 'C' THEN 12
        WHEN 'D' THEN 13
        WHEN 'E' THEN 14
        WHEN 'F' THEN 15
        ELSE CONVERT(INT, @SingleCode)
       END

  SET @ResultVar = @ResultVar + dbo.[10Transfer2_16](@Ten)
  SET @I = @I + 1

 END

 RETURN RTRIM(LTRIM(@ResultVar))
END

EX:Select dbo.[16Transfer2]('F')==>1111

沒有留言: