I need to replace a certain character inside a field with an empty string.
But this simple need becomes complicated because I need to extract data from a remote Oracle server and I need a parameter to reduce the amount of data returned.
So I need to use dynamic SQL with a properly constructed string.
This is the code I tried to solve the problem
DECLARE @sql varchar(4000) DECLARE @Period varchar(8) = '20250301' SET @sql = 'INSERT INTO [dbo].[TmpTable] (CustomerID) SELECT ORA.CustomerID FROM OPENQUERY(ORACLE_SERVER, '' SELECT -- this is where I want to remove the § character REPLACE(CustIdentifier, ''''§'''', '''') AS CustomerID FROM CustomerTable WHERE StartDate>= ''''' + @period + ''''' '') ORA' EXEC (@SQL) As is, the code above returns this error:
OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE_SERVER" returned message "ORA-01756: quoted string not properly terminated".
Msg 7323, Level 16, State 2, Line 2
An error occurred while submitting the query text to OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE_SERVER".

PRINT @sqlfor troubleshooting. Doing so will show you have too many quotes. TryINSERT INTO [dbo].[TmpTable] ( CustomerID ) SELECT ORA.CustomerID FROM OPENQUERY(ORACLE_SERVER, '' SELECT -- this is where i want to remove the § character REPLACE(CustIdentifier, ''§'', '''') AS CustomerID FROM CustomerTable WHERE StartDate>= ''' + @period + ''' '') ORA'NULL). You might need to useeTO_DATEfor the date literal depending on the Oralce column data type.TRANSLATE()? While Microsoft SQL Server requires the same number of characters in the from_string and to_string parameters, on Oracle if there is no replacement character in the to_string parameter then matching from_string characters are deleted from the result, e.g.:SELECT TRANSLATE('_It''s full of §§§§§§§§§stars_', '_§', '_') FROM DUAL;db<>fiddle