Find all numbers in a string | Database Journal

Find all numbers in a string

Jun 8, 2009
1 minute read

>>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: mak_999@yahoo.com
–Date: Feb 2, 2004declare 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 <=@len1
	begin
	set @char1 =substring(@inputstring,@count1,1)
	if ascii(@char1) between 48 and 57
	begin
	set @word=@word+substring(@inputstring,@count1,1)
	end
	set @count1=@count1+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

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.