# 金額阿拉伯數字轉換為中文的預存程序（轉）

@ChangeMoney Money
as
Set Nocount ON
Declare @String1 char(20)
Declare @String2 char(30)
Declare @String4 Varchar(100)
Declare @String3 Varchar(100) --從原A值中取出的值
Declare @i int --迴圈變數
Declare @J Int --A的值乘以100的字串長度
Declare @Ch1 Varchar(100) --數位漢語讀法
Declare @Ch2 Varchar(100) --數字位的漢字讀法
Declare @Zero Int --用來計算連續有幾個零
Declare @ReturnValue VarChar(100)

Select @ReturnValue = ''
Select @String1 = '零壹貳三肆伍陸柒捌玖'
Select @String2 = '萬仟佰拾億仟佰拾萬仟佰拾元角分'

Select @String4 = Cast(@ChangeMoney*100 as int)

select @J=len(cast((@ChangeMoney*100) as int))

Select @String2=Right(@String2,@J)

Select @i = 1

while @i<= @j Begin

Select @String3 = Substring(@String4,@i,1)

if @String3<>'0' Begin

Select @Ch1 = Substring(@String1, Cast(@String3 as Int) + 1, 1)
Select @Ch2 = Substring(@String2, @i, 1)
Select @Zero = 0 --表示本位不為零
end
else Begin
If (@Zero = 0) Or (@i = @J - 9) Or (@i = @J - 5) Or (@i = @J - 1)
Select @Ch1 = '零'
Else
Select @Ch1 = ''

Select @Zero = @Zero + 1 --表示本位為0

--如果轉換的數值需要擴大，那麼需改動以下運算式 I 的值。
Select Ch2 = ''

If @i = @J - 10 Begin
Select @Ch2 = '億'
Select @Zero = 0
end

If @i = @J - 6 Begin
Select @Ch2 = '萬'
Select @Zero = 0
end

if @i = @J - 2 Begin
Select @Ch2 = '元'
Select @Zero = 0
end

If @i = @J
Select @Ch2 = '整'

end

Select @ReturnValue = @ReturnValue + @Ch1 + @Ch2

select @i = @i+1
end

--最後將多餘的零去掉
If CharIndex('仟仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '仟仟', '仟')

If CharIndex('佰佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '佰佰', '佰')

If CharIndex('零元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零元', '元')

If CharIndex('零萬',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零萬', '萬')

If CharIndex('零億',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零億', '億')

If CharIndex('零整',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零整', '整')

If CharIndex('零佰',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零佰', '零')

If CharIndex('零仟',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '零仟', '零')

If CharIndex('元元',@ReturnValue) <> 0
Select @ReturnValue = Replace(@ReturnValue, '元元', '元')

Select @ReturnValue
GO

## E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD