Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Extract only numbers from a String

Author  Topic 

mperson007
Starting Member

1 Post

Posted - 2010-08-03 : 09:58:06
I have a table with 7176 rows. I need to extract only numbers from one of the columns that currently has text and numbers in it. How do I extract only numbers from the values in this column? Thanks in advance.

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-03 : 10:20:51
"It's 5:00 Somewhere"


SET NOCOUNT ON

DECLARE @x varchar(max); SET @x = 'abc12efg34'
DECLARE @t table (seq int IDENTITY(1,1), c char(1))
DECLARE @l int; SET @l = LEN(@x)
DECLARE @n int; SET @n = 1
DECLARE @numStr varchar(max); SET @numStr = ''

WHILE @n < @l + 1
BEGIN
INSERT INTO @t(c) SELECT SUBSTRING(@x,@n,1)
SET @n = @n + 1
END

SELECT @numStr = @numStr + c
FROM @t
WHERE c IN ('1','2','3','4','5','6','7','8','9','0')
ORDER BY seq

SELECT @numStr

SET NOCOUNT ON



You could turn this into a user defined function



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-04 : 07:20:40
or
http://beyondrelational.com/blogs/madhivanan/archive/2007/12/18/extract-only-numbers-from-a-string.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-08-04 : 08:39:10
I have simplified madhivanan's solution a little bit.Hope he does not feel offended :)


declare @x varchar(max); SET @x = 'abc12efg34'
declare @numbers varchar(max)=''

select @numbers=@numbers + '' + number from
(
select
convert(varchar(2),substring(@x,number,1))as number
from master.dbo.spt_values where type='p'
and number between 1 and LEN(@x)
)t where number between '0' and '9'

select @numbers as numbers



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-09 : 02:54:42
quote:
Originally posted by Idera

I have simplified madhivanan's solution a little bit.Hope he does not feel offended :)


declare @x varchar(max); SET @x = 'abc12efg34'
declare @numbers varchar(max)=''

select @numbers=@numbers + '' + number from
(
select
convert(varchar(2),substring(@x,number,1))as number
from master.dbo.spt_values where type='p'
and number between 1 and LEN(@x)
)t where number between '0' and '9'

select @numbers as numbers



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH


No Problem

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-08-09 : 03:07:59
quote:
Originally posted by madhivanan


No Problem

Madhivanan

Failing to plan is Planning to fail



Thanks


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-09 : 13:27:20
Wel...that's pretty darn close...

but why

and number between 1 and LEN(@x)


???

why not 0 and 9..you're still looking at 1 byte at a time

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-08-09 : 15:45:59
quote:
Originally posted by X002548

Wel...that's pretty darn close...

but why

and number between 1 and LEN(@x)


???

why not 0 and 9..you're still looking at 1 byte at a time

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam





I'd guess so you do not compare an out of bounds index. As spt_values will give you values that'll be outside the dounds of the string.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-08-09 : 15:48:13
It has same logic with madhivanan approach. No need scalar UDF. Tally table for splitting, FOR XML PATH() for concatenating.
;WITH CTE(i) AS
(SELECT 'sadlfjks 213 dfj 234324 sf' UNION ALL
SELECT '2342sadfwe4234sdf9o7234' UNION ALL
SELECT '2432sfsf234')

SELECT D.i
FROM CTE
CROSS APPLY
(SELECT i + ''
FROM (SELECT SUBSTRING(i, n, 1)
FROM Numbs /* Tally Tables */
WHERE n <= LEN(i)
)D(i)
WHERE i LIKE '[0-9]'
FOR XML PATH(''))D(i);
/*
i
-----------------
213234324
2342423497234
2432234
*/


______________________
Go to Top of Page
   

- Advertisement -