Home > Enterprise >  Need replace text with single quotes in SQL
Need replace text with single quotes in SQL

Time:05-18

I need to make a script that make a replacement of all the registers of a column that contains: '@xxxx', removing the single quotes. (xxxx could be empty or any string. The @, is neccesary, the simple quotes too).

I'm implementing something like this, but it doesn't work for me, because the 'REPLACE' function accepts Strings as parameters, instead of accepting other functions.

Thanks

I tried

UPDATE MY_TABLE SET MY_COLUMN = REPLACE('MY_COLUMN',
    SELECT "MY_COLUMN" FROM MY_TABLE WHERE "MY_COLUMN" like '%''@%', 
        SELECT TRIM( '%''%' FROM MY_TABLE WHERE "MY_COLUMN" like '%''@%'))

CodePudding user response:

Its unclear what dbms you are using, but here is what I would so in SQLServer, you can modify for you dbms if not this.

They key point here is you are doing this the wrong way round, you use the Replace function on each row, not pass the record set into the replace.

UPDATE mt
SET myColumn =  REPLACE(myColumn, '''','')
FROM myTable mt 
WHERE myColumn LIKE '%''@%'
  • Related