CREATE FUNCTION dbo.StripCharacters (@AString NVARCHAR(MAX), @AMatchExpression VARCHAR(255)) RETURNS NVARCHAR(MAX) AS
/*
  Created by Kent Johansen, April 2015.
  Usage:
    Alphabetic only: SELECT dbo.StripCharacters('a1!s2@d3#f4$', '^a-z')
    Numeric only: SELECT dbo.StripCharacters('a1!s2@d3#f4$', '^0-9')
    Alphanumeric only: SELECT dbo.StripCharacters('a1!s2@d3#f4$', '^a-z0-9')
    Non-alphanumeric: SELECT dbo.StripCharacters('a1!s2@d3#f4$', 'a-z0-9')
*/
BEGIN
  SET @AMatchExpression =  '%['+@AMatchExpression+']%'

  WHILE PatIndex(@AMatchExpression, @AString) > 0
    SET @AString = Stuff(@AString, PatIndex(@AMatchExpression, @AString), 1, '')

  RETURN @AString
END