急!截去字符串并转换为数字类型!谢谢!!!
字段 A(VARCHAR) B(numeric)
PWW*20 10
PRRW*12 11
PW*100 30
WWEC*1000 17
VB*5 50
PIOVB*5 30
要求是,截取字段 A * 右边的数字(星号右边全是数字) ,和字段 B 相乘,后求和!
结果:=20*10+12*11+100*30+1000*17+5*50+5*30
只求结果!!!
谢谢各位大侠!!!
问题点数:50、回复次数:9Top
1 楼califord(远方)回复于 2006-05-04 15:51:10 得分 0
具体不会,只感觉与文件取名一样Top
2 楼wwh999(印钞机V2.0...开发中....)回复于 2006-05-04 15:58:36 得分 0
use pubs
go
Declare @Tb table(A varchar(20),B int)
insert @Tb select 'PWW*20',10
union all select 'PRRW*12',11
union all select 'PW*100',3
union all select 'WWEC*1000',17
union all select 'VB*5',50
union all select 'PIOVB*5',30
select * from @Tb
----------------------------------------------------------
select cast(substring(A,charindex('*',A)+1,len(A))*B as int) 位置, B from @Tb
Top
3 楼wwh999(印钞机V2.0...开发中....)回复于 2006-05-04 15:59:44 得分 0
以上是取得*号右边的数字Top
4 楼wwh999(印钞机V2.0...开发中....)回复于 2006-05-04 16:04:30 得分 0
把上面的代码贴到查询分析器中,F5运行可得到结果,你看看是不是.起作用的select是这句:
---------------------------------------------------------------------------------
select cast(substring(A,charindex('*',A)+1,len(A))*B as int) 位置, B from @TbTop
5 楼azsoft(Try my best)回复于 2006-05-04 16:19:46 得分 0
select sum(cast(substring(A,charindex('*',A)+1,len(A))*B as int)) as 求和, B from 表Top
6 楼azsoft(Try my best)回复于 2006-05-04 16:21:06 得分 0
select sum(cast(substring(A,charindex('*',A)+1,len(A)) as int)* B) as 求和 from 表Top
7 楼wwh999(印钞机V2.0...开发中....)回复于 2006-05-04 16:22:07 得分 0
use pubs
go
--插入测试数据
Declare @Tb table(A varchar(20),B int)
insert @Tb select 'PWW*20',10
union all select 'PRRW*12',11
union all select 'PW*100',3
union all select 'WWEC*1000',17
union all select 'VB*5',50
union all select 'PIOVB*5',30
select * from @Tb --查询得到表信息
------------------------------------------------------------
--取出*号后面的数字,B列
select cast(substring(A,charindex('*',A)+1,len(A))*B as int) [*号后的数字], B B列 from @Tb
--汇总数据,得到A数字*B列的汇总
select sum(cast(substring(A,charindex('*',A)+1,len(A)) as int) * B) as 总数 from @Tb
Top
8 楼wwh999(印钞机V2.0...开发中....)回复于 2006-05-04 16:46:47 得分 0
有多个*号的情况,从右边取!
--------------------------------------------------------------------------------
use pubs
go
Declare @Tb table(A varchar(20),B int)
insert @Tb select 'PWW*20',10
union all select 'PRRW*12',11
union all select 'PW*100',3
union all select 'WWEC*1000',17
union all select 'VB*5',50
union all select 'PIOVB*5',30
select * from @Tb --查询得到表信息
------------------------------------------------------------
--取出*号后面的数字,B列,使用reverse函数反转字符串进行的操作
select A,reverse(A) 反转值 ,reverse(left(reverse(A),charindex('*',reverse(A))-1)) as 截得的字串,B B列 from @TB
--得到汇总值
select sum(cast(reverse(left(reverse(A),charindex('*',reverse(A))-1)) as int)* B) as 泄总求和 from @Tb
Top
9 楼xeqtr1982(Visual C# .NET)回复于 2006-05-04 18:03:16 得分 0
declare @t table(a varchar(10),b numeric)
insert into @t select 'PWW*20',10
union all select 'PRRW*12',11
union all select 'PW*100',30
union all select 'WWEC*1000',17
union all select 'VB*5',50
union all select 'PIOVB*5',30
select sum(right(a,charindex('*',reverse(a))-1)*b) as [sum] from @tTop




