对字段值类似于"$10.0340;$10.4300;$10.6500;$30.9600;"的操作
Posted on 2004-11-12 14:48 小二哥 阅读(397) 评论(0) 编辑 收藏 举报
出处:http://community.csdn.net/Expert/topic/3530/3530351.xml?temp=.1452448
name(nvarchar 255) price(nvarchar 255)
书1 $10.0340;$10.4300;$10.6500;$30.9600;
书2 $5.2750;$5.7579;$8.3600;
现在领导要我新建一个字段"price2"
将price1中的价格乘以0.8 更新到price2中,格式还是要像price中的一样$XX.XXXX;$XX.XXXX;
--函数:
--创建测试表tb:
--运行结果:
name price price2
书1 $10.0340;$10.4300;$10.6500;$30.9600; $8.0272;$8.344;$8.52;$24.768;
书2 $5.2750;$5.7579;$8.3600; $4.22;$4.60632;$6.688;
name(nvarchar 255) price(nvarchar 255)
书1 $10.0340;$10.4300;$10.6500;$30.9600;
书2 $5.2750;$5.7579;$8.3600;
现在领导要我新建一个字段"price2"
将price1中的价格乘以0.8 更新到price2中,格式还是要像price中的一样$XX.XXXX;$XX.XXXX;
--函数:
create function dbo.fn_m(@vchstring varchar(1000))
returns varchar(1000)
as
begin
declare @intstart int,@intlocation int
declare @s varchar(1000),@vchsubstring varchar(100)
select @intstart =1,@s=''
select @intlocation = charindex(';',@vchstring,@intstart)
while (@intlocation <>0 )
begin
select @vchsubstring=CAST(substring(@vchstring,@intstart,@intlocation-@intstart) AS FLOAT) * 0.8
select @s=@s+';$'+cast(@vchsubstring as varchar)
select @intstart = @intlocation +1
select @intlocation = charindex(';',@vchstring,@intstart)
end
return(RIGHT(RTRIM(@s),LEN(RTRIM(@s))-1))
end
returns varchar(1000)
as
begin
declare @intstart int,@intlocation int
declare @s varchar(1000),@vchsubstring varchar(100)
select @intstart =1,@s=''
select @intlocation = charindex(';',@vchstring,@intstart)
while (@intlocation <>0 )
begin
select @vchsubstring=CAST(substring(@vchstring,@intstart,@intlocation-@intstart) AS FLOAT) * 0.8
select @s=@s+';$'+cast(@vchsubstring as varchar)
select @intstart = @intlocation +1
select @intlocation = charindex(';',@vchstring,@intstart)
end
return(RIGHT(RTRIM(@s),LEN(RTRIM(@s))-1))
end
--创建测试表tb:
create table tb([name] nvarchar (255), price nvarchar(255))
insert into tb
select '书1','$10.0340;$10.4300;$10.6500;$30.9600;' union all
select '书2','$5.2750;$5.7579;$8.3600;'
select *,price2=dbo.fn_m(replace(price,'$',''))+';' from tb
drop table tb
insert into tb
select '书1','$10.0340;$10.4300;$10.6500;$30.9600;' union all
select '书2','$5.2750;$5.7579;$8.3600;'
select *,price2=dbo.fn_m(replace(price,'$',''))+';' from tb
drop table tb
--运行结果:
name price price2
书1 $10.0340;$10.4300;$10.6500;$30.9600; $8.0272;$8.344;$8.52;$24.768;
书2 $5.2750;$5.7579;$8.3600; $4.22;$4.60632;$6.688;