Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip non-numeric characters from a string

I'm currently doing a data conversion project and need to strip all alphabetical characters from a string. Unfortunately I can't create or use a function as we don't own the source machine making the methods I've found from searching for previous posts unusable.

What would be the best way to do this in a select statement? Speed isn't too much of an issue as this will only be running over 30,000 records or so and is a once off statement.

like image 688
Michael A Avatar asked Sep 20 '12 00:09

Michael A


3 Answers

You can do this in a single statement. You're not really creating a statement with 200+ REPLACEs are you?!

update tbl
set S = U.clean
from tbl
cross apply
(
    select Substring(tbl.S,v.number,1)
    -- this table will cater for strings up to length 2047
    from master..spt_values v
    where v.type='P' and v.number between 1 and len(tbl.S)
    and Substring(tbl.S,v.number,1) like '[0-9]'
    order by v.number
    for xml path ('')
) U(clean)

Working SQL Fiddle showing this query with sample data

Replicated below for posterity:

create table tbl (ID int identity, S varchar(500))
insert tbl select 'asdlfj;390312hr9fasd9uhf012  3or h239ur ' + char(13) + 'asdfasf'
insert tbl select '123'
insert tbl select ''
insert tbl select null
insert tbl select '123 a 124'

Results

ID  S
1   390312990123239
2   123
3   (null)
4   (null)
5   123124
like image 119
RichardTheKiwi Avatar answered Oct 16 '22 10:10

RichardTheKiwi


CTE comes for HELP here.

;WITH CTE AS 
(
SELECT 

      [ProductNumber] AS OrigProductNumber
      ,CAST([ProductNumber] AS VARCHAR(100)) AS [ProductNumber]           
FROM [AdventureWorks].[Production].[Product]
UNION ALL
SELECT OrigProductNumber
       ,CAST(STUFF([ProductNumber], PATINDEX('%[^0-9]%', [ProductNumber]), 1, '') AS VARCHAR(100) ) AS [ProductNumber]
FROM CTE WHERE PATINDEX('%[^0-9]%', [ProductNumber]) > 0 
)

SELECT * FROM CTE
WHERE PATINDEX('%[^0-9]%', [ProductNumber]) = 0   
OPTION (MAXRECURSION 0)

output:

OrigProductNumber   ProductNumber
WB-H098                 098
VE-C304-S               304
VE-C304-M               304
VE-C304-L               304
TT-T092                 092
like image 5
ClearLogic Avatar answered Oct 16 '22 11:10

ClearLogic


RichardTheKiwi's script in a function for use in selects without cross apply, also added dot because in my case I use it for double and money values within a varchar field

CREATE FUNCTION dbo.ReplaceNonNumericChars (@string VARCHAR(5000))
RETURNS VARCHAR(1000)
AS 
    BEGIN
        SET @string = REPLACE(@string, ',', '.')
        SET @string = (SELECT   SUBSTRING(@string, v.number, 1)
                       FROM     master..spt_values v
                       WHERE    v.type = 'P'
                                AND v.number BETWEEN 1 AND LEN(@string)
                                AND (SUBSTRING(@string, v.number, 1) LIKE '[0-9]'
                                     OR SUBSTRING(@string, v.number, 1) LIKE '[.]')
                       ORDER BY v.number
                      FOR
                       XML PATH('')
                      )
        RETURN @string
    END
GO

Thanks RichardTheKiwi +1

like image 3
Pierre Avatar answered Oct 16 '22 11:10

Pierre