Knowledge base / SQL
SQL REPLACE: substitution order and case sensitivity
I show why replacing SQL also changes MySQL and how input collation affects SQL Server REPLACE.

REPLACE substitutes occurrences of a specified substring with another string. I use it when I can state exactly what should change. I do not assume that it recognises whole words, understands product names or coordinates the intentions behind several consecutive substitutions.
The basic examples here use PostgreSQL 18. I separately identify SQL Server comparison settings and MySQL 8.4 behaviour. All strings are synthetic.
Three arguments and every occurrence
The PostgreSQL documentation describes replace as replacing all occurrences of a substring.
SELECT replace('old model / old label', 'old', 'new') AS cleaned;
The result is new model / new label. The arguments specify the input, the search fragment and the replacement, in that order. An empty replacement removes the matched fragment, such as hyphens in a code when the code definition permits that change.
SELECT replace('AB-12-XY', '-', '') AS compact_code;
This gives AB12XY. It is a valid character transformation, but it does not prove that the compact code remains unique. Another product coded AB12-XY would collapse to the same value. I check meaning as well as the resulting text.
REPLACE does not recognise word boundaries
Consider this expression:
SELECT replace('Learn SQL and MySQL', 'SQL', 'Databases') AS first_step;
It gives Learn Databases and MyDatabases. SQL also occurs inside MySQL, so both occurrences change. The function has no reason to treat the second occurrence as part of a name I intended to preserve.
Replacing only a standalone name needs a more precise rule, such as structured fields or a carefully designed regular expression. Switching functions does not remove the need to define a word within the source format.
Read nested replacements from the inside out
SELECT replace(
replace('Learn SQL and MySQL', 'SQL', 'Databases'),
'MySQL', 'PostgreSQL'
) AS final_text;
The inner expression produces Learn Databases and MyDatabases. The outer expression searches for MySQL, which is no longer present. The final result therefore remains Learn Databases and MyDatabases; it does not end with PostgreSQL.
Merely reversing these two substitutions also needs care. PostgreSQL contains SQL, so the following step could modify the newly inserted name. I write down the input and output of each step before combining expressions. For a larger mapping, I consider keeping names in a dedicated field and mapping complete values.

Check case behaviour for the actual engine
MySQL 8.4 specifies case-sensitive matching within REPLACE. In SQL Server, comparisons depend on the input collation, as documented by Microsoft. One case-sensitivity rule should therefore not be presented as universal SQL behaviour.
These SQL Server expressions explicitly choose comparison rules for simple ASCII characters.
SELECT REPLACE('apple APPLE' COLLATE Latin1_General_CS_AS,
'apple', 'orange') AS case_sensitive;
SELECT REPLACE('apple APPLE' COLLATE Latin1_General_CI_AS,
'apple', 'orange') AS case_insensitive;
The expected outputs are orange APPLE and orange orange, respectively. CS indicates case sensitivity and CI case insensitivity. This example does not establish a complete normalisation policy for every alphabet or accented character.
Inspect NULL and long-string behaviour
In SQL Server and MySQL, null in any argument produces null. If a missing description represents missing information, I preserve it. Replacing missing descriptions with empty strings is a separate data-model decision rather than an automatic part of substitution.
SQL Server also limits the result to 8000 bytes when the input expression is not varchar(max) or nvarchar(max). Its REPLACE documentation explains explicitly casting the input to a suitable large-value type for longer results. Bytes and characters differ, and a large destination column cannot automatically repair truncation that occurred earlier in an expression.
Preview a proposed change with SELECT
Assume products contains product_id and description. I first prepare a comparison in PostgreSQL:
SELECT product_id,
description AS original_description,
replace(description, 'old model', 'new model') AS proposed_description
FROM products
ORDER BY product_id;
SELECT calculates a query result without storing this substitution in products. I inspect the original and proposal together, including unchanged records. Persisting a change requires a separately established scope and reversal method. This entry stays with read-only expressions.
Choose an operation matching the task
For substring substitution, I use REPLACE. For mapping individual characters, I consider TRANSLATE, available in PostgreSQL and SQL Server since 2017, among other engines. SQL Server STUFF deletes a specified length at a specified position and inserts replacement text. Position-based editing answers a different question from searching for a fragment.
For practice, write down the input, search fragment and output of each nested substitution. Add a case where the replacement contains the fragment searched by the next step. Predicting that effect helps prevent accidental name changes. For finding a fragment’s location, continue with POSITION.
Key takeaways
Does REPLACE match only whole words?
No. It searches for a substring, so SQL is also found within MySQL. Word boundaries need a separately designed rule.
Is REPLACE always case-sensitive?
There is no universal engine rule. MySQL documents case-sensitive matching, while SQL Server uses the input collation.
Why does nesting order matter?
The outer replacement receives the inner result. An earlier step can remove a later search fragment or insert another fragment which a subsequent step changes.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
