Contents

1 10讌襯 2 ~ 36讌 譴 襦 覲
2 2讌襯 10讌襦 覲


1 10讌襯 2 ~ 36讌 譴 襦 覲 #

ALTER FUNCTION dbo.ufn_DecToDigit (@Dec BIGINT, @base TINYINT)
RETURNS VARCHAR(63) --
AS
/******************************************************************************************
	煙:	2005-12-01
	煙:	http://www.DataBaser.Net/
	SP企:	ufn_DecToDigit
	SP伎:	
		-10讌襯 2 ~ 36讌 譴 襦 覲 

	:
		SELECT dbo.ufn_DecToDigit(17, 2) -- 10讌 17 2讌襦 覲
		SELECT dbo.ufn_DecToDigit(17, 8) -- 10讌 17 8讌襦 覲
		SELECT dbo.ufn_DecToDigit(17, 13) -- 10讌 17 13讌襦 覲

	ル:
		@ItemInfo BIGINT : 
	, 	@base TINYINT
	覦覲:
	谿語^覓語: 
	觜螻: 
	EDIT HISTORY:
*********************************************************************************************/
BEGIN
	--ル 螳 2讌襦 襴
	DECLARE 
		@r AS VARCHAR(63)
	,	@alldigits AS VARCHAR(36)
	--SET @base = 2  --2讌覃 2, 8讌覃 8, 16讌覃 16
	SET @alldigits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	SET @r = ''

	WHILE @Dec > 0
	BEGIN
		SET @r = substring(@alldigits, @Dec % @base + 1, 1) + @r
		SET @Dec = @Dec / @base; 
	END
	 
	RETURN @r
END
GO

2 2讌襯 10讌襦 覲 #

ALTER FUNCTION dbo.ufn_DigitToDec (@Digit VARCHAR(15))
RETURNS INT
AS
/******************************************************************************************
	煙:	2005-12-01
	煙:	http://www.DataBaser.Net/
	SP企:	ufn_DigitToDec
	SP伎:	
		-2讌襯 10讌襦 覲 

	:
		SELECT dbo.ufn_DigitToDec('0010')

	ル:

	覦覲:
	谿語^覓語: 
	觜螻: 
	EDIT HISTORY:
*********************************************************************************************/
BEGIN
	DECLARE @Len INT, @i INT, @r INT, @DigitNum VARCHAR(15)
	SET @Len = LEN(@Digit)
	SET @i = 1
	SET @r = 0
	SET @DigitNum = REVERSE(@Digit)

	WHILE (@i <= @Len)
	BEGIN
		SELECT @r = @r + CASE WHEN 1 = SUBSTRING(@DigitNum, @i, 1) THEN POWER(2, @i-1) ELSE 0 END
		SET @i = @i + 1 
	END

	RETURN @r
END
GO