Remove special characters from string in SQL server


I faced many times an issue to remove special characters from a string. Suppose you are working on searching concept and you have to remove the special characters from search string due to query performance, there are many solutions is available but T-SQL is quickly resolved this issue.

Following query may help you to resolve your issue.


DECLARE @str VARCHAR(400)
DECLARE @expres  VARCHAR(50) = '%[~,@,#,$,%,&,*,(,),.,!]%'
SET @str = '(remove) ~special~ *characters. from string in sql!'
  WHILE PATINDEX( @expres, @str ) > 0
        BEGIN
         SET @str = Replace(REPLACE( @str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),''),'-',' ')
        END
SELECT @str




Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...