日期:2014-05-17  浏览次数:20368 次

怎么查询字段里的数字
例如

col

asd123
5315
53115dg
6181ert1531

结果
col

123
5315
53115
61811531

------解决方案--------------------
create Function [dbo].[fn_Number] (@texto varchar(100))
returns varchar(100)
as
begin
declare @s varchar(100),@i int
declare @v varchar(1)
set @i=1
set @s=''
while @i<=len(@texto)
begin
set @v=substring(@texto,@i,1)
set @s=@s+case when isnumeric(@v)=1 then @v else '' end
set @i=@i+1
end
return @s
End 

GO


;WITH cte AS (
select 'asd123' as col
union all select '5315'
union all select '53115dg'
union all select '6181ert1531'
)
select dbo.fn_number(col) from cte 

/*
123
5315
53115
61811531
*/