SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite String Functions / SQLite REPLACE Function

SQLite REPLACE Function

Summary: in this tutorial, you will learn how to use SQLite REPLACE() function to replace all occurrences of a specified string with another string.

Introduction to SQLite REPLACE() function

The SQLite REPLACE() function is a string function that allows you to replace all occurrences of a specified string with another string.

The following shows the syntax of the REPLACE() function:

REPLACE(string,pattern,replacement)

In this syntax:

  • string is the string that you want to perform the replacement.
  • pattern is the substring to be found in the original string
  • replacement is the replacement string.

The REPLACE() function is useful for updating character data in a table e.g., update the dead links and typos.

SQLite REPLACE() function examples

Let’s take some examples of using the REPLACE() function.

SQLite REPLACE() function simple example

The following example replaces the string 'A' in the string 'AA B CC AAA' with the new string 'Z':

SELECT REPLACE('AA B CC AAA','A','Z');

The following picture shows the output:

The following statement uses the REPLACE() function to replace the string 'This' in the string 'This is a cat' with the new string 'That':

SELECT REPLACE('This is a cat','This','That');

The output is:

SQLite REPLACE Function simple example

SQLite REPLACE() string in a table example

We will use the REPLACE() function to replace strings in a table.

First, create a new table named contacts that consists of four columns: contact_id, first_name, last_name, and phone:

CREATE TABLE contacts ( contact_id INT PRIMARY KEY, first_name TEXT NOT NULL, last_name TEXT NOT NULL, phone TEXT NOT NULL );

Next, insert into the contacts table some rows:

INSERT INTO contacts(first_name, last_name, phone) VALUES('John','Doe','410-555-0168'); INSERT INTO contacts(first_name, last_name, phone) VALUES('Lily','Bush','410-444-9862');

Then, query data from the contacts table:

SELECT first_name, last_name, phone FROM contacts;

Here are the contents of the contacts table:

SQLite REPLACE Function sample table

After that, update the phone area code from local (410) to international one(+1-410):

UPDATE contacts SET phone = REPLACE(phone,'410','+1-410');

Finally, query data from the contacts table to verify if the phone has been updated:

SELECT first_name, last_name, phone FROM contacts;
SQLite REPLACE Function updating table example

As shown clearly in the output, the phone numbers have been updated successfully.

In this tutorial, you have learned how to use the SQLite REPLACE() function to replace all occurrences of a substring in a string with a new string.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Rtrim
Next SQLite Substr

SQLite String Functions

  • INSTR
  • LENGTH
  • LOWER
  • LTRIM
  • RTRIM
  • REPLACE
  • SUBSTR
  • TRIM
  • UPPER

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All Rights Reserved.