sql-serverreplaceerror-handling

Replace single quotes in SQL Server


I have this function in SQL Server to replace single quotes.

But when I insert a single quote it throws an error on Replace(@strip,''','')):

Create Function [dbo].[fn_stripsingleQuote]
    (@strStrip varchar(Max))
    returns varchar
as
begin
    declare @CleanString varchar(Max)
    SET @var=(Replace(@strip,'',''))

    return @var
end

Solution

  • You need to double up your single quotes as follows:

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