ALTER FUNCTION [dbo].[fn_AlgorismToOther]
(
@BaseString VARCHAR(50),
@Value BIGINT,
@StringLength INT
)
RETURNS varchar(50)
AS
--select dbo.fn_AlgorismToOther ('0123456789abcdef',52185+1,8) as m
BEGIN
DECLARE @m_ReturnString varchar(50),
@m_Base BIGINT,
@m_Position INT
SET @m_ReturnString=''
----Check the valid data
IF ISNUMERIC(@Value)=0
BEGIN
RETURN @m_ReturnString
END
SET @m_Base=LEN(RTRIM(LTRIM(@BaseString)))
IF @m_Base=0 OR @m_Base=1
BEGIN
RETURN @m_ReturnString
END
IF (@m_Base>30 AND @StringLength >12) OR (@m_Base>40 AND @StringLength >10) ----if it is too big,it will raise the arithmetic overflow error
BEGIN
RETURN @m_ReturnString
END
IF @Value > POWER(@m_Base,@StringLength)
BEGIN
RETURN @m_ReturnString
END
SET @m_ReturnString = SUBSTRING(@BaseString,@Value % @m_Base +1,1)
SET @Value = FLOOR(@Value/@m_Base)
WHILE @Value > 0
BEGIN
SET @m_ReturnString = SUBSTRING(@BaseString,@Value % @m_Base +1,1) + @m_ReturnString
SET @Value = FLOOR(@Value/@m_Base)
END
SET @m_ReturnString=REPLICATE('0',@StringLength-LEN(@m_ReturnString)) + @m_ReturnString
RETURN @m_ReturnString
END
调用
dbo.Fn_algorismtoother('0123456789ABCDEFGHJKLMNPRSTVWXYZ', @serialno, 3)