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 ONDECLARE @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 = 1DECLARE @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 seqSELECT @numStrSET NOCOUNT ON You could turn this into a user defined functionBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
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 |
 |
|
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 MadhivananFailing to plan is Planning to fail |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-09 : 03:07:59
|
quote: Originally posted by madhivanan No Problem MadhivananFailing to plan is Planning to fail
Thanks Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
X002548
Not Just a Number
15586 Posts |
|
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 whyand number between 1 and LEN(@x)???why not 0 and 9..you're still looking at 1 byte at a timeBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd 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. |
 |
|
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-----------------21323432423424234972342432234*/ ______________________ |
 |
|
|