Replace single quotes in SQL Server Replace single quotes in SQL Server sql-server sql-server

Replace single quotes in SQL Server


You need to double up your single quotes as follows:

REPLACE(@strip, '''', '')


Try REPLACE(@strip,'''','')

SQL uses two quotes to represent one in a string.


If you really must completely strip out the single quotes you can do this:

Replace(@strip, '''', '')

However, ordinarily you'd replace ' with '' and this will make SQL Server happy when querying the database. The trick with any of the built-in SQL functions (like replace) is that they too require you to double up your single quotes.

So to replace ' with '' in code you'd do this:

Replace(@strip, '''', '''''')

Of course... in some situations you can avoid having to do this entirely if you use parameters when querying the database. Say you're querying the database from a .NET application, then you'd use the SqlParameter class to feed the SqlCommand parameters for the query and all of this single quote business will be taken care of automatically. This is usually the preferred method as SQL parameters will also help prevent SQL injection attacks.