CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

如何取得字段中的数字??菜鸟发问!

楼主liangx326(selina)2005-06-01 17:36:17 在 MS-SQL Server / 基础类 提问

大家好!  
  我想在某个字段(varchar类型)的内容中取出数字,请问大虾们如何处理阿?  
  比如:  
  字段:column1  
  类型:varchar(255)  
  内容:我要送给你1001朵玫瑰  
   
   
  我现在要提取出这个1001,该如何处理阿?  
  大虾们帮忙啊! 问题点数:50、回复次数:5Top

1 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 17:49:46 得分 5

单是提1001就简单了。  
  select   substring(column1,charindex('1',column1),4)    
  from   yourtableTop

2 楼duanduan1122(俺村俺帅!!!)回复于 2005-06-01 17:50:15 得分 5

若是有许多行就复杂了。Top

3 楼xluzhong(Ralph)回复于 2005-06-01 18:03:37 得分 20

declare   @a   table   (col1   varchar(255))  
  insert   into   @a   select   '我要送给你1001朵玫瑰'  
  insert   into   @a   select   '我要送给你999朵玫瑰'  
   
  declare   @col1   varchar(255)  
  declare   test   cursor   for   select   col1   from   @a  
  open   test  
  fetch   next   from   test  
  into   @col1  
  while   @@fetch_status=0  
  begin  
      declare   @return   varchar(255)  
      set   @return=''  
      while   patindex('%[0-9]%',@col1)>0  
          begin  
              set   @return=@return+substring(@col1,patindex('%[0-9]%',@col1),1)  
              set   @col1=stuff(@col1,patindex('%[0-9]%',@col1),1,'')  
          end  
      select   @return  
      print   @return  
      fetch   next   from   test  
      into   @col1  
  end  
  CLOSE   test  
  DEALLOCATE   testTop

4 楼zbyh331(我才刚上路耶!)回复于 2005-06-01 18:15:53 得分 20

declare   @a   table   (col1   varchar(255))  
  insert   into   @a   select   '我要送给你1001朵玫瑰'  
  insert   into   @a   select   '我要送给你999朵玫瑰'  
   
  declare   @col1   varchar(255)  
  declare   test   cursor   for   select   col1   from   @a  
  open   test  
  fetch   next   from   test  
  into   @col1  
  while   @@fetch_status=0  
  begin  
      declare   @return   varchar(255)  
      set   @return=''  
      while   patindex('%[0-9]%',@col1)>0  
          begin  
              set   @return=@return+substring(@col1,patindex('%[0-9]%',@col1),1)  
              set   @col1=stuff(@col1,patindex('%[0-9]%',@col1),1,'')  
          end  
      select   @return  
      print   @return  
      fetch   next   from   test  
      into   @col1  
  end  
  CLOSE   test  
  DEALLOCATE   test  
  Top

5 楼liangx326(selina)回复于 2005-06-02 10:46:15 得分 0

谢谢各位,结贴了!Top

相关问题

  • 菜鸟发问:在线等,有关字段查询问题!!!急!解答就有分1!
  • 菜鸟发问
  • 菜鸟发问!
  • 菜鸟发问
  • 菜鸟的问题---字段显示
  • 菜鸟发问,好赚啊!!!!
  • 菜鸟开始发问(一)
  • 菜鸟开始发问(二)
  • 菜鸟开始发问(三)
  • 菜鸟发问DropDownList问题

关键词

  • 字段
  • col1
  • 玫瑰
  • patindex
  • varchar
  • declare
  • fetch
  • 要送给你
  • insert
  • column

得分解答快速导航

  • 帖主:liangx326
  • duanduan1122
  • duanduan1122
  • xluzhong
  • zbyh331

相关链接

  • SQL Server类图书

广告也精彩

反馈

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