读书人

怎么提取字段中的数字

发布时间: 2012-01-15 22:57:48 作者: rapoo

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

读书人网 >SQL Server

热点推荐