小写金额转换成大写金额的procedure源码
--drop proc ConvertToUpperCase
/*declare @a varchar(50) --@a为欲转换的大写金额
exec ConvertToUpperCase 1000000,@a output --将123456789.56转换成大写金额
select @a --在QA中查看效果
*/
/*declare @a varchar(50) --@a为欲转换的大写金额
exec ConvertToUpperCase 1056031010000,@a output
select @a */
if exists (select * from sysobjects where objectproperty(object_id('ConvertToUpperCase'), 'IsProcedure') = 1)
drop procedure ConvertToUpperCase
GO
create procedure ConvertToUpperCase
(@mExpense Money,
@sRetUpperMoney varchar(50) OutPut)
as
begin
declare @sExpense varchar(20),@sLittle varchar(4),@sFixed varchar(15)
declare @nDot Integer,@nLenAll Integer,@sUpperMoney varchar(30),@nStep Integer
declare @cChar char(1),@sUpperNumbers varchar(30),@sNumber char(2),@sTemp varchar(50)
declare @sZero char(2),@nIsZeroLast Integer,@sth varchar(10),@nFixed Integer
declare @sMoney char(2)
if @mExpense=0
begin
select @sRetUpperMoney='零元'
return
end
if @mExpense<0
begin
select @sMoney='负'
select @mExpense=abs(@mExpense)
end
else
select @sMoney=''
select @sUpperMoney = '佰拾万仟佰拾亿仟佰拾万仟佰拾元' --加大范围,这里设计len(@sUpperMoney)<=15,再大这种算法就不适合了。
select @sUpperNumbers= '玖捌柒陆伍肆叁贰壹零'
select @sZero = '零'
select @sExpense = convert(varchar(20),@mExpense)
select @nDot = CharIndex('.',@sExpense)
select @nLenAll = Len(@sExpense)
if @nDot > 0
begin
select @sFixed = substring(@sExpense,1,@nDot -1)
select @sLittle = substring(@sExpense,@nDot + 1,@nLenAll - @nDot)
end
else
select @sFixed = @sExpense
select @nFixed = Len(@sFixed)
select @nStep = @nFixed
select @sTemp = ''
select @nIsZeroLast = 0
if @sFixed <> '0'
while @nStep > 0
begin
--while @nStep > 0
select @cChar = substring(@sFixed,@nStep,1)
select @sNumber = substring(@sUpperNumbers,10 - convert(int,@cChar),1)
select @sth = substring(@sUpperMoney,len(@sUpperMoney) - (Len(@sFixed) - @nStep),1)
if @cChar <> '0'
begin
--if @cChar <> '0'
if @nIsZeroLast = 0
select @sTemp = @sNumber + @sth + @sTemp
if @nIsZeroLast = 1
begin
select @sTemp = @sNumber + @sth + @sZero + @sTemp
select @nIsZeroLast = 0
end
if @nIsZeroLast = 2
begin
select @sTemp = @sNumber + @sth + @sTemp
select @nIsZeroLast = 0
end
end --if @cChar <> '0'
else
--@cChar = '0'
begin
if @nIsZeroLast <> 2
begin
--@nIsZeroLast <> 2
if (@sth = '万') or (@sth = '亿') or (@sth = '元')
begin
if @nIsZeroLast = 1
begin
select @sTemp = @sth + @sZero + @sTemp
if substring(@sTemp,1,3)='亿万零' --修改
select @sTemp=stuff(@sTemp,1,3,'亿零')
if substring(@sTemp,1,2)='万零' --修改
select @sTemp=stuff(@sTemp,1,2,'零')
select @nIsZeroLast = 2
end
else
begin
select @sTemp = @sth + @sTemp
if substring(@sTemp,1,2)='万元' and @nStep>4--修改
select @sTemp=stuff(@sTemp,1,2,'元')
if substring(@sTemp,1,3)='亿万元'
select @sTemp=stuff(@sTemp,1,3,'亿元')
select @nIsZeroLast = 2
end
end
else
select @nIsZeroLast = 1
end
else
--@nIsZeroLast = 2
begin
if (@sth = '仟')
select @nIsZeroLast = 0
end
end --@nIsZeroLast <> 2
select @nStep = @nStep - 1
end --while @nStep > 0
declare @sTempLittle varchar(10),@sFirst char(1),@sSecond char(1)
select @sFirst = '',@sSecond = ''
if Len(@sLittle) = 2
begin
select @sFirst = substring(@sLittle,1,1),@sSecond = substring(@sLittle,2,1)
end
if Len(@sLittle) = 1
begin
select @sFirst = substring(@sLittle,1,1)
end
select @sTempLittle = ''
if (@sFirst <> '') and (@sFirst <> '0')
begin
select @sNumber = substring(@sUpperNumbers,10 - convert(int,@sFirst),1)
select @sTempLittle = @sTempLittle + @sNumber + '角'
end
if (@sSecond <> '') and (@sSecond <> '0')
begin
select @sNumber = substring(@sUpperNumbers,10 - convert(int,@sSecond),1)
select @sTempLittle = @sTempLittle + @sNumber + '分'
end
select @sRetUpperMoney = @sMoney + @sTemp + @sTempLittle
end