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 */
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
*/