CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

急!截去字符串并转换为数字类型!谢谢!!!

楼主zglwxb(假钞换贞操)2006-05-04 15:33:53 在 MS-SQL Server / 基础类 提问

字段       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

相关问题

关键词

得分解答快速导航

  • 帖主:zglwxb

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo