Link to home
Start Free TrialLog in
Avatar of jen_jen_jen
jen_jen_jen

asked on

MSSQL: Replace non-numeric characters in a field?

I want to find and delete any non-numeric characters in a field.  Rather than typing out:

UPDATE table SET field = REPLACE(field,'/','')

For every character, is there a way to quickly do this?
Avatar of David Todd
David Todd
Flag of New Zealand image

Hi,

Other than what you've suggested, I'd write a function called isdigit and simply loop through the string character at a time - if isdigit returns false then do the replace. Alternatively copy the digits to the end of a new string ...

HTH
  David

PS A CLR might be the fastest way to do this.
You can't do that and here is why:

12 is numeric
1.2 is numeric
1..2 is not numeric

but how can you possible tell that in 1..2 the correct number is 12 or 1.2? There is no way do do that with code. Only a human can decide what value is correct in a situation like this.!

You need to identify all the non-numeric values for that column ad deal with them manually.

That's why a correct design and ahead planing is of such importance.

To identify the non-umeric values:


SELECT 
	col
from 
	YourTable
where
	isnumeric(col)<>1

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial