日期:2014-05-19  浏览次数:20506 次

如何提取字段中的数字?
已知一字段的记录如下:
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
*/