Для больших наборов данных мне повезло больше с этой функцией, которая проверяет значение ASCII. Я добавил опции для сохранения только буквенных, цифровых или буквенно-цифровых символов в зависимости от параметров.
--CleanType 1 - Remove all non alpanumeric
-- 2 - Remove only alpha
-- 3 - Remove only numeric
CREATE FUNCTION [dbo].[fnCleanString] (
@InputString varchar(8000)
, @CleanType int
, @LeaveSpaces bit
) RETURNS varchar(8000)
AS
BEGIN
-- // Declare variables
-- ===========================================================
DECLARE @Length int
, @CurLength int = 1
, @ReturnString varchar(8000)=''
SELECT @Length = len(@InputString)
-- // Begin looping through each char checking ASCII value
-- ===========================================================
WHILE (@CurLength <= (@Length+1))
BEGIN
IF (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 48 and 57 AND @CleanType in (1,3) )
or (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 65 and 90 AND @CleanType in (1,2) )
or (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 97 and 122 AND @CleanType in (1,2) )
or (ASCII(SUBSTRING(@InputString,@CurLength,1)) = 32 AND @LeaveSpaces = 1 )
BEGIN
SET @ReturnString = @ReturnString + SUBSTRING(@InputString,@CurLength,1)
END
SET @CurLength = @CurLength + 1
END
RETURN @ReturnString
END