0

I have a table with a column of string. within the string there are single quote which I want to get rid of all single quotes.for example:

"''hey, hey, we're the monkees''"

my regex works perfect and select all the values containing single quotes.

select regexp_replace(colName, '%''%', '') from tblName;

but it does not update my table when I want to replace this regex with nothing.

UPDATE tblName SET colName = regexp_replace(colName, '%''%', '');

I also checked this one

UPDATE tblName SET colName = replace(colName, '%''%', '');

2 Answers 2

2

Different functions and operators in Postgres use one of three different pattern matching languages, as described in a dedicated section of the manual.

The % form you are using here is the SQL LIKE syntax, where % represents "any number of any character". But the function you are using, regexp_replace, expects a Posix regular expression, where the equivalent would be .* (. meaning any character, * meaning repeat zero or more times).

Also note that LIKE expressions have to match the whole string, but a Posix regex doesn't, unless you explicitly match the start of the string with ^ and the end with $.

So the direct translation of '%''%' would be '^.*''.*$', giving you this:

UPDATE tblName SET colName = regexp_replace(colName, '^.*''.*$', '');

In practice, this would give the same effect as the simpler:

UPDATE tblName SET colname='' WHERE colname LIKE '%''%';

Your actual use case is much simpler: you want to replace all occurrences of a fixed string (', which will need to be quoted and escaped as '''') with another fixed string (the empty string, written ''). So you don't need any pattern matching at all, just straight replacement using replace:

UPDATE tblName SET colname=replace(colname, '''', '');

This will probably be faster if you limit it to rows that contain an apostrophe to begin with:

UPDATE tblName SET colname=replace(colname, '''', '') WHERE colname LIKE '%''%';
Sign up to request clarification or add additional context in comments.

5 Comments

this one remove the values but I just want to remove the single quotes!
@Raha1986 I've updated the answer based on your clarification, please read to the end.
that is funny! the same query does not work for "(". I thought it should be the same as single quotes!
could you please tell that the same query should not work for "(" or ")"
I couldn't tell you without seeing exactly what you're trying, but UPDATE tblName SET colname=replace(colname, '(', '') WHERE colname LIKE '%(%'; should work just fine. Maybe you just made a mistake somewhere.
0

% is not an regexp character

try this

select regexp_replace(colName, $$'$$, '','g') from tblName;

($$ is use to surround your string instead of ' to simplify the query) (,'g') is use to continue after the first quote is found.

 UPDATE tblName SET colName = regexp_replace(colName, $$'$$, '','g');

7 Comments

Baron there is something wrong about the query, after the first quote, it makes rest of the query, purpleish
execute it anyway, editors do not recognize $$ but postgresql serveur recognise it, it's just a display problem. if you want you can use regexp_replace(colName, '''', '','g') but it's not very readable
I want to just remove the single quotes not remove whole the value!
I think the readability is a matter of taste. On a short string like this, I find $$'$$ harder to read than '''', and it has the bonus of being the same in other DBMSes.
@RémyBaron To be really pedantic, it's what's used by a lot of clients when displaying functions, and commonly used by humans writing functions; inside the DB, the definition is a string value like any other, and doesn't need to be quoted or escaped. But absolutely, for that case dollar-quoting is an absolute godsend; I tend to think of it as the Postgres equivalent of "heredocs", and use it in similar circumstances. I would absolutely never use it as a defence against injection, though; that's just security through obscurity, because it's probably no harder to inject a $ than a '.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.