March 3rd, 2014, 12:37 AM
Remove non alphanumeric characters
I have created this function that will remove all non apha numeric characters, except spaces.
This function is working fine, but because of this function, the execution has become very slow, it takes more than double time to execute a query. Can someone tell me, how i can optimize this whole thing?
ALTER FUNCTION dbo.StripNonAlphaNumerics
DECLARE @p INT = 1, @n VARCHAR(MAX) = '';
DECLARE @TEMP NVARCHAR(MAX)
WHILE @p <= LEN(@s)
SET @TEMP = SUBSTRING(@s, @p, 1)
IF (@TEMP LIKE '[A-Za-z0-9]' OR @TEMP = ' ')
SET @n += @TEMP;
SET @p += 1;
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!