Tags

Below is the function created by Damian Edwards to strips out non-numeric characters, after doing that I can convert to numeric safely (after checking for empty strings and nulls of course):

— Function to strip out non-numeric chars
ALTER FUNCTION dbo.UDF_ParseNumericChars
(
@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @IncorrectCharLoc SMALLINT
–SET @IncorrectCharLoc = PATINDEX(‘%[^0-9A-Za-z]%’, @string)
SET @IncorrectCharLoc = PATINDEX(‘%[^0-9.]%’, @string)
WHILE @IncorrectCharLoc > 0
BEGIN
SET @string = STUFF(@string, @IncorrectCharLoc, 1, ”)
SET @IncorrectCharLoc = PATINDEX(‘%[^0-9.]%’, @string)
END
SET @string = @string
RETURN @string
END
GO

Advertisements