REPLACE()

SQL · String Functions

Theory

REPLACE(string, old, new) returns a copy of string where every occurrence of old is replaced with new. REPLACE('98290-12345', '-', '') removes the dash and returns '9829012345'. All occurrences are replaced, not just the first, and if old is not found the string comes back unchanged. Use an empty string as new to delete text.

REPLACE is available under the same name in almost every database (SQLite, MySQL, Postgres, SQL Server, Oracle, Snowflake). It matches exact substrings, and the match is case-sensitive in most databases, so REPLACE(x, 'it', 'IT') will not touch 'It'. To replace several different things, nest the calls: REPLACE(REPLACE(phone, '-', ''), ' ', '') removes both dashes and spaces. For pattern-based replacement (any digit, any run of spaces), many databases offer REGEXP_REPLACE. Used in an UPDATE, REPLACE can clean a whole column in one statement.

Why we use it

Imported data is full of unwanted characters and outdated values: dashes and spaces in phone numbers, old domain names, wrong abbreviations. REPLACE fixes them in bulk without writing any code outside SQL.

When to use it

Use REPLACE to remove or swap a fixed piece of text. Nest calls for a few different pieces. For single-character mappings use TRANSLATE where available; for patterns use REGEXP_REPLACE.

How it works

  1. Take the string, the old text and the new text
  2. Search for the next occurrence of old
  3. Found an occurrence? Yes: Swap it for new and keep searching after it. No: Stop searching.
  4. Nested REPLACE runs the inner call first
  5. Return the cleaned string

Syntax

REPLACE(string, old, new)

Example

SELECT REPLACE(email, '@techtown.in', '@company.com') FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic