SQLite is a database engine. It is a serverless architecture as it does not require any server to process queries. Since it is serverless, it is lightweight and preferable for small datasets. It is used to develop embedded software. It is cross-platform and available for various Operating systems such as Linux, macOS, Windows, Android, and so on.
Replace Statement
REPLACE Statement is a type of Statement that helps us to replace one part of a string with another string. It is an INSERT OR REPLACE command which means if the record already exists in the table then it replaces the substring with a new string, but if the record does not exist in the table then INSERT new record into the table. The REPLACE Statement is similar to the INSERT statement. We will understand everything with the help of examples.
Syntax:
SELECT REPLACE(A,B,C)
- A -> Original String present in the Table.
- B -> Substring or string that we need to replace.
- C -> Replace string.
Examples 1:
Let's say we have an 'ABC DEF ANM ADK DLM' string and we have to replace substring 'A' with 'S' String. here we use REPLACE Statement.
SELECT REPLACE('ABC DEF ANM ADK DLM', 'A', 'S')
Output:
SBC DEF SNM SDK DLM
Explanation: In the output you can clearly see that all substring where earlier 'A' lies but after REPLACE statement it change with 'S' string.
Example 2:
If the second string does not exist then it replace nothing and returns the original string.
SELECT REPLACE('ABC DEF ANM ADK DLM', ' LA', 'S')
Output:
ABC DEF ANM ADK DLM
Explanation: In the above query we are trying to replace all substring 'LA' with 'S'. Their is no subtsring with 'LA', hence, it returns the original string.
Example 3:
What if we are trying to find substring which is empty string and we want to replace it with another string. Does REPLACE work with empty string ? Let's understand .
SELECT REPLACE('ABC DEF ANM ADK DLM', ' ', 'S')
Output:
ABC DEF ANM ADK DLM
Explanation: In the above query we are trying to replace string with the empty substring. But REPLACE statement is not work with empty string so it return Original string and nothing will change.
Example with Table
Let's say we have a Employee table and a coulumn named employee name. Now we want to replace 'BC' with '##' evrytime when BC appears in empname coloumn. To do this we can use the REPLACE statement.
Employee TableSELECT empid,empname,city,REPLACE(empname, 'BC', '##') newName FROM Employee
Output:
After REPLACE OperationExplanation: In the above query, we have replaced all empname whose name contains 'BC' as substring with '##'. After performing the REPLACE statement our new employee table will look the above table.
Similar Reads
SQL SELECT INTO Statement The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read
PL/SQL UPDATE Statement The UPDATE statement in the PL/SQL(Procedural Language/ Structural Query Language) is the powerful SQL (Structured Query Language) command used to modify the existing data in the database table. In this article, we will explain the PL/SQL UPDATE Statement, its syntax, and examples in detail.PL/SQL U
6 min read
MySQL - ALTER VIEW Statement The ALTER VIEW statement in MySQL is a powerful tool that allows users to modify the definition of an existing view without the need to drop and recreate it. This statement is particularly useful for changing the query or structure of a view to better help the needs of the application or database de
5 min read
SQL Server SELECT INTO Statement SQL Server is a relational database management system. SQL Server offers robust security features to protect data integrity and confidentiality. It includes authentication, authorization, encryption, and various mechanisms to secure the database environment. It is designed to scale from small applic
6 min read
SQL UPDATE Statement In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read