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 DELETE Statement
The SQL DELETE statement is one of the most commonly used commands in SQL (Structured Query Language). It allows you to remove one or more rows from the table depending on the situation. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the tabl
4 min read
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
MySQL DELETE Statement
In DBMS, CRUD operations (Create, Read, Update, Delete) are essential for effective data management. The Delete operation is crucial for removing data from a database. This guide covers the MySQL DELETE statement, exploring its syntax and providing examples. Understanding how DELETE works helps ensu
6 min read
PL/SQL DELETE Statement
In PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
6 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match.In this article, we will learn how IN operator works and provide practical examples to help you be
3 min read
PL/SQL INSERT Statement
The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
3 min read
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
PostgreSQL MERGE Statement
The MERGE statement in PostgreSQL is a powerful data manipulation tool introduced in PostgreSQL 15, enabling conditional INSERT, UPDATE, and DELETE operations in a single command. This feature streamlines data synchronization between tables, making it ideal for tasks such as upserts and handling tab
4 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