Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Jun 8, 2009

Find all numbers in a string

By DatabaseJournal.com Staff



>>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, 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 <=@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



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM