I have created this function that will remove all non apha numeric characters, except spaces.

Code:
ALTER FUNCTION dbo.StripNonAlphaNumerics
(
  @s VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
  DECLARE @p INT = 1, @n VARCHAR(MAX) = '';
  DECLARE @TEMP NVARCHAR(MAX)
  WHILE @p <= LEN(@s)
  BEGIN
	SET @TEMP = SUBSTRING(@s, @p, 1)
    IF (@TEMP LIKE '[A-Za-z0-9]' OR @TEMP = ' ')
    BEGIN
      SET @n += @TEMP;
    END 
    SET @p += 1;
  END
  RETURN(@n);
END
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?