首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么提取字段中的数字

2012-01-15 
如何提取字段中的数字?已知一字段的记录如下:00x1000200003现在想同个一函数得到如下的效果:123++++++++++

如何提取字段中的数字?
已知一字段的记录如下:
00x1
0002
00003
现在想同个一函数得到如下的效果:
1
2
3
++++++++++++++++++++++++++++
我测试这样来搞好像不成
  select   stuff( '00x1 ',1,patindex( '%[0-9]% ', '00x1 ')-1, ' ')


[解决办法]
declare @t table(name varchar(10))
insert @t
select '00x1 ' union all
select '0002 ' union all
select '00003 '

select convert(int,stuff(name,1,patindex( '%[^0-9]% ',name), ' ')) as name
from @t

/*结果
name
-----------
1
2
3
*/

热点排行