Archive for April 30th, 2025

SQL: sed-like replacement of a string

Wednesday, April 30th, 2025

Imagine the situation: you have an SQL table with some string in some column. There is a plenty of such rows, and you want to replace this string in each row with some other string, something like in Bash – sed ‘s/green apple/blue apple/g’.

The original table looks like this.

Select:

Result:

We want to change “from ACME” to “moved from ACME”.

First, let’s test our SQL statement with SELECT, no changes will be made:

Now do an SQL UPDATE in a sed-like style:

The result after an UPDATE:

So, to test the statement, do:

To replace, do:

If you need case-insensitive, use ILIKE instead of LIKE.