--聲明字符型變量保存輸入金額
@revsstrmoney varchar(15), --逆序字符
@len0 smallint, --全部數字長度
@adig char(1), --存放單個數字
@i integer
select @i= 1
select @convmoney = convert(numeric(12,2),@convmoney) --強制轉換為 numeric(12,2)格式
select @strmoney = convert(varchar(15),@convmoney)
select @revsstrmoney = reverse(@strmoney)
select @len0 = datalength(@strmoney)
while @i<=@len0
begin
select @adig = substring(@revsstrmoney,@i,1)
update #tmp1 set num = @adig where id = @i
select @i = @i+1
end
update #tmp1 set dx = b.dx from #tmp1 a , #tmp0 b
where a.num = b.digital
/*
select id,num,dx,unit,dx+unit as dx_unit from #tmp1 where num>='0' and num<='9'
order by id desc
*/
declare @dx_unit varchar(12) ,@result varchar(255)
select @result = ''
declare cursor1 cursor for
select dx+unit as dx_unit from #tmp1 where num>='0' and num<='9'
order by id desc for read only
open cursor1
fetch cursor1 into @dx_unit
while @@SQLSTATUS =0
begin
select @result = @result +ltrim(rtrim(@dx_unit))
fetch cursor1 into @dx_unit
end
close cursor1
DEALLOCATE cursor cursor1
--select @result
while(charindex('零億',@result)>0)
begin
select @result = stuff(@result,charindex('零億',@result) ,4,'億')
end
while(charindex('零萬',@result)>0)
begin
select @result = stuff(@result,charindex('零萬',@result) ,4,'萬')
end
while(charindex('零仟',@result)>0)
begin
select @result = stuff(@result,charindex('零仟',@result) ,4,'零')
end
while(charindex('零佰',@result)>0)
begin
select @result = stuff(@result,charindex('零佰',@result) ,4,'零')
end
while(charindex('零拾',@result)>0)
begin
select @result
@result) ,4,'元')
end
while(charindex('零萬',@result)>0)
begin
select @result = stuff(@result,charindex('零萬',@result) ,4,'萬')
end
while(charindex('零角',@result)>0)
begin
select @result = stuff(@result,charindex('零角',@result) ,4,'零')
end
while(charindex('零分',@result)>0)
begin
select @result = stuff(@result,charindex('零分',@result) ,4,'零')
end
while(charindex('零零',@result)>0)
begin
select @result = stuff(@result,charindex('零零',@result) ,4,'零')
end
while(charindex('億萬',@result)>0)
begin
select @result = stuff(@result,charindex('億萬',@result) ,4,'億')
end
if @convmoney = convert(numeric(12,0),@convmoney) --整數
select @result = substring( @result , 1 , charindex('元',@result)+1)+'整'
select @result= ltrim(rtrim(@result))
--解決只有小數的情況
select @result = ltrim(rtrim(@result))
if