SQL REPLACE is a string function that returns a new value after substituting every occurrence of a search pattern with a replacement pattern. Use it in SELECT to clean data or in UPDATE to overwrite table values. Syntax: REPLACE(source_string, search_text, replace_text). Case-sensitive in most databases.
SQL REPLACE substitutes every instance of a search pattern with new text, making string cleanup fast and declarative.
SQL REPLACE returns a new string where all occurrences of search_text inside source_string are swapped with replace_text. Nothing is modified in the table unless REPLACE appears inside an UPDATE statement.
Most SQL engines use REPLACE(source_string, search_text, replace_text)
. The function is deterministic: identical inputs always produce identical outputs, aiding testability.
REPLACE inside SELECT lets analysts strip unwanted characters or reformat codes without touching the underlying data. This approach is ideal for dashboards and ad-hoc reporting.
Remove parentheses and dashes by nesting REPLACE calls: REPLACE(REPLACE(phone,'-',''),'(', '')
. Each function acts on the previous result, giving a digit-only string.
Wrap REPLACE in an UPDATE: UPDATE customers SET notes = REPLACE(notes, 'ACME', 'Acme Inc.');
. Always test with SELECT first to avoid unintended mass changes.
Case sensitivity depends on the database’s collation. MySQL with a case-insensitive collation treats “abc” and “ABC” as identical, while PostgreSQL is case-sensitive by default.
Use REGEXP_REPLACE when you need pattern matching or wildcards, such as stripping all non-numeric characters. SQL REPLACE only handles literal strings.
Yes. Apply TRIM first to remove leading/trailing whitespace, then use REPLACE to swap interior characters. This two-step approach yields cleaner results.
Always preview changes with SELECT, back up data before UPDATE, and limit UPDATE with a WHERE clause. Document why the replacement is needed for future maintainers.
Galaxy’s AI copilot autocompletes nested REPLACE calls, previews changes, and lets teams endorse cleanup queries in shared collections, eliminating copy-paste confusion.
SQL REPLACE is the go-to tool for straightforward string substitution. Combine it with UPDATE for permanent fixes, test thoroughly, and lean on modern editors like Galaxy for safe, rapid iteration.
Yes, every major engine—MySQL, PostgreSQL, SQL Server, Oracle, Snowflake—supports REPLACE or an equivalent function.
If source_string is NULL, the function returns NULL. Wrap the column in COALESCE to avoid unexpected NULL results.
Pass the newline character: REPLACE(comment, CHR(10), ' ')
in Oracle or REPLACE(comment, CHAR(10), ' ')
in MySQL.
On indexed columns, REPLACE in WHERE prevents index use. In SELECT, performance is usually negligible unless scanning large text blobs.