复杂问题求解?

mogu 2003-09-22 03:31:06
数据库CASE表中有一字段TOTAL MONEY类型,现把字段里的价格转换成人民币的大写
形式的价格,把结果存在S_TOTAL的字段中,写一个存储过程实现此功能!
比如132.46
写成壹佰叁十贰元肆角陆分
...全文
37 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ropriest 2003-09-22
  • 打赏
  • 举报
回复
呵呵,收藏,说不定哪天也要用上一把,免得自己再去写了!
txlicenhe 2003-09-22
  • 打赏
  • 举报
回复
其实大部分问题都是重复的。
zjcxc 元老 2003-09-22
  • 打赏
  • 举报
回复
调用这个函数来更新就行了.

update CASE set S_TOTAL=dbo.GetChineseNum([TOTAL])
zjcxc 元老 2003-09-22
  • 打赏
  • 举报
回复
CREATE FUNCTION GetChineseNum (@inputId money)
RETURNS Nvarchar(4000) AS
BEGIN
declare @rV Nvarchar(4000)

declare @tmpstr Nvarchar(4000),@M Nvarchar(4000),@K Nvarchar(4000),@I numeric(38,2),
@J int,@lastJ int,@LastV Nvarchar(10),@LastF Nvarchar(10),@LastE Nvarchar(10),@LastVE Nvarchar(10)
set @I=@inputId
select @tmpstr=N'零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿拾佰仟',@K=N'',@M=cast(cast(@I*100 as bigint) as varchar(800)),@J=len(@M),@LastVE=N''

while @J>=1
begin
set @LastF=substring(@tmpstr, cast(substring(@m,len(@M)-@j+1,1) as bigint)+1,1)
set @LastE=substring(@tmpstr,10+@J,1)
if @LastF<>N'零'
begin
if @LastV=N'零'
if (@lastJ>=7 and @j<=7) or (@lastJ>=11 and @j<=11 ) or (@lastJ>=3 and @j<=2)
if @J<=2 and @lastJ<=3
set @K=@K+@LastVE+@LastF+@LastE
else
set @K=@K+@LastVE+@LastV+@LastF+@LastE
else
set @K=@K+@LastV+@LastF+@LastE
else
set @K=@K+@LastF+@LastE
select @lastJ=@j,@LastVE=N''
end
else
begin
if @LastVE=N'' and @lastJ>11 set @LastVE=N'亿'
if @LastVE=N'' and @lastJ>7 and @lastJ<10 set @LastVE=N'万'
if @LastVE=N'' and @lastJ>3 and @lastJ<6 set @LastVE=N'元'
if @LastV<>N'零' set @lastJ=@j
end
set @LastV=@LastF
set @J=@J-1
end
if @lastJ>=3 set @K=@K+N'元'
if @lastJ>=2 set @K=@K+N'整'

set @rv=@K

return @rv
END
伍子V5 2003-09-22
  • 打赏
  • 举报
回复
原来别人已经写了,真是好东西呀,收藏
伍子V5 2003-09-22
  • 打赏
  • 举报
回复
有些麻烦,有空再跟你做了:)
yujohny 2003-09-22
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2226/2226734.xml?temp=.5441858
这个帖子里有你要的全部东西
aierong 2003-09-22
  • 打赏
  • 举报
回复


遗憾,我从来不写存储过程。放个函数在这里。


if object_id('py') is not null drop function py
go
create function py(@input nvarchar(1000)) returns varchar(1000) with encryption as
begin
declare @output varchar(1000),@p int,@chn nchar(1)
set @output=''
set @p=1
while @p<=len(@input)
begin
set @chn=substring(@input,@p,1)
set @output=@output+ case when @chn>'阼' COLLATE Chinese_PRC_CI_AS then '.'
when @chn<'吖' COLLATE Chinese_PRC_CI_AS then '.'
when @chn>='匝' COLLATE Chinese_PRC_CI_AS then 'z'
when @chn>='鸭' COLLATE Chinese_PRC_CI_AS then 'y'
when @chn>='熙' COLLATE Chinese_PRC_CI_AS then 'x'
when @chn>='娃' COLLATE Chinese_PRC_CI_AS then 'w'
when @chn>='獭' COLLATE Chinese_PRC_CI_AS then 't'
when @chn>='飒' COLLATE Chinese_PRC_CI_AS then 's'
when @chn>='蚺' COLLATE Chinese_PRC_CI_AS then 'r'
when @chn>='旗' COLLATE Chinese_PRC_CI_AS then 'q'
when @chn>='趴' COLLATE Chinese_PRC_CI_AS then 'p'
when @chn>='噢' COLLATE Chinese_PRC_CI_AS then 'o'
when @chn>='拿' COLLATE Chinese_PRC_CI_AS then 'n'
when @chn>='妈' COLLATE Chinese_PRC_CI_AS then 'm'
when @chn>='啦' COLLATE Chinese_PRC_CI_AS then 'l'
when @chn>='咖' COLLATE Chinese_PRC_CI_AS then 'k'
when @chn>='鸡' COLLATE Chinese_PRC_CI_AS then 'j'
when @chn>='铪' COLLATE Chinese_PRC_CI_AS then 'h'
when @chn>='旮' COLLATE Chinese_PRC_CI_AS then 'g'
when @chn>='发' COLLATE Chinese_PRC_CI_AS then 'f'
when @chn>='垩' COLLATE Chinese_PRC_CI_AS then 'e'
when @chn>='沓' COLLATE Chinese_PRC_CI_AS then 'd'
when @chn>='礤' COLLATE Chinese_PRC_CI_AS then 'c'
when @chn>='疤' COLLATE Chinese_PRC_CI_AS then 'b'
else 'a'
end
set @p=@p+1
end
return @output
end
aierong 2003-09-22
  • 打赏
  • 举报
回复


Create Procedure AtoC
@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

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧