Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all occurrences of a substring in a database text field

I have a database that has around 10k records and some of them contain HTML characters which I would like to replace.

For example I can find all occurrences:

SELECT * FROM TABLE
WHERE TEXTFIELD LIKE '%&#47%'

the original string example:

this is the cool mega string that contains &#47

how to replace all &#47 with / ?

The end result should be:

this is the cool mega string that contains /

like image 985
Andrew Avatar asked Jun 12 '12 01:06

Andrew


People also ask

How replace all occurrences of a string in SQL?

SQL Server REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive. Tip: Also look at the STUFF() function.

How do you UPDATE a substring in SQL?

Update Part of a String in SQL Using the REPLACE() Function It replaces all occurrences of a specified string value with another string value. The REPLACE function returns a string where it replaces a substring with another substring.

How do I replace multiple characters in a string in SQL Server?

Using the REPLACE() function will allow you to change a single character or multiple values within a string, whether working to SELECT or UPDATE data.


2 Answers

If you want to replace a specific string with another string or transformation of that string, you could use the "replace" function in postgresql. For instance, to replace all occurances of "cat" with "dog" in the column "myfield", you would do:

UPDATE tablename
SET myfield = replace(myfield,"cat", "dog")

You could add a WHERE clause or any other logic as you see fit.

Alternatively, if you are trying to convert HTML entities, ASCII characters, or between various encoding schemes, postgre has functions for that as well. Postgresql String Functions.

like image 52
davesnitty Avatar answered Nov 08 '22 06:11

davesnitty


The answer given by @davesnitty will work, but you need to think very carefully about whether the text pattern you're replacing could appear embedded in a longer pattern you don't want to modify. Otherwise you'll find someone's nooking a fire, and that's just weird.

If possible, use a suitable dedicated tool for what you're un-escaping. Got URLEncoded text? use a url decoder. Got XML entities? Process them though an XSLT stylesheet in text mode output. etc. These are usually safer for your data than hacking it with find-and-replace, in that find and replace often has unfortunate side effects if not applied very carefully, as noted above.

It's possible you may want to use a regular expression. They are not a universal solution to all problems but are really handy for some jobs.

If you want to unconditionally replace all instances of "&#47" with "/", you don't need a regexp.

If you want to replace "&#47" but not "&#471", you might need a regexp, because you can do things like match only whole words, match various patterns, specify min/max runs of digits, etc.

In the PostgreSQL string functions and operators documentation you'll find the regexp_replace function, which will let you apply a regexp during an UPDATE statement.

To be able to say much more I'd need to know what your real data is and what you're really trying to do.

like image 36
Craig Ringer Avatar answered Nov 08 '22 08:11

Craig Ringer