Find all numbers in a string

>>Script Language and Platform: SQL Server
This function finds all the numbers in a string.

Example.

select dbo.udf_findallnumbers (’12sfg34′)
–results
1234
select dbo.udf_findallnumbers (‘asas123’)
–results
123
select dbo.udf_findallnumbers (‘assd123jdh556jdfd4j56j78’)
–results
12355645678
select dbo.udf_findallnumbers (‘sadasd’)
–results
0
select dbo.udf_findallnumbers (‘sadasds65’)
–results
65

create table test (name varchar(100))
insert into test select ‘A2b4b2b5bb6bb8bb9’
insert into test select ‘MAK9974’
insert into test select ‘Eiko36DKoike’

select dbo.udf_findallnumbers (name) from test
–results
2425689
9974
36

Author: MAK



Create function dbo.udf_findallnumbers (@inputstring varchar(100))
returns bigint
as
begin
–Author : MAK
–Contact: [email protected]
–Date: Feb 2, 2004
–declare variables
declare @count1 smallint
declare @len1 smallint
declare @word varchar(100)
declare @char1 char
–Assignment
set @word=”
set @count1=1
set @len1 = datalength(@inputstring)

While @count1 <[email protected]
begin
set @char1 =substring(@inputstring,@count1,1)
if ascii(@char1) between 48 and 57
begin
set @[email protected]+substring(@inputstring,@count1,1)
end
set @[email protected]+1
end
return convert(bigint,@word)

end



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Latest Articles