How to UPDATE and REPLACE Part of a String in PL/SQL?
Last Updated :
06 Feb, 2024
PL/SQL is a procedural language designed to enable developers to combine the power of procedural language with Oracle SQL. It is developed by Oracle and serves as one of the three key programming languages embedded in the Oracle database, alongside SQL and Java. PL/SQL includes procedural language elements such as conditions and loops and can handle exceptions (run-time errors). It also allows the declaration of constants and variables, procedures, functions, packages, types and variables of those types, and triggers.
In this article, we are going to see how we can update a part of the string with some other value in PL/SQL. Understanding this can enable the developer to manipulate strings in a much more complex fashion in their tables.
Before looking at how we can go about doing the foretold task, let us have a look at the REPLACE() function.
PL/SQL REPLACE String Function
The REPLACE function is used to replace all the occurrences of a substring with some other string.
Syntax:
REPLACE(string, old_substring, new_substring)
Parameters:
- string: The string in which to replace.
- old_substring: The substring to be replaced.
- new_substring: The substring to replace the old substring.
Example of REPLACE() Function
The following query replaces "World" with "GeeksforGeeks"
SELECT REPLACE("Hello World!", "World", "GeeksforGeeks") as Greeting;
Output:
Replaced outputExplanation:
The given SQL query utilizes the REPLACE function to modify the string "Hello World!" by replacing every occurrence of the substring "World" with "GeeksforGeeks". The resulting output assigned the alias "Greeting", is "Hello GeeksforGeeks!" – a string where the specified substitution has been applied, effectively altering the original greeting.
Examples of Updating Column by Replacing Substring in PL/SQL
Let's start by creating a table and adding some sample data to the table. We create an EMPLOYEE table which contains fields like empId, name, and the email of the person. The following query creates the table:
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
);
Now that we have the table, let's add some data to the table. The following query adds three employee records to the table:
INSERT INTO EMPLOYEE VALUES (0001, 'Clark', '[email protected]');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', '[email protected]');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', '[email protected]');
Output:
Initial dataUpdating Column with REPLACE
We can use the REPLACE function in several ways. For this, we are going to use it in conjunction with the UPDATE clause to replace the domain of the email from 'some.com' to 'domain.net'.
The following is the query that does the trick of doing the job for us.
UPDATE EMPLOYEE SET email=REPLACE(email, 'some.com', 'domain.net');
The following is the data of the table after executing the above query:
Output:
Updated dataExplanation:
The email domain for each employee has been updated from 'some.com' to 'domain.net'. The REPLACE function ensures that occurrences of 'some.com' in the email column are replaced with 'domain.net'.
As you can see the email of each employee has changed from [email protected] to [email protected].
Conclusion
The UPDATE statement in PL/SQL is a simple way to change specific segments of string values in a single column. This is especially useful if you need to make large-scale updates or make regular changes across many records in your PL/SQL database. Always make sure to back up your data before running any update statement to avoid any unwanted consequences.
Similar Reads
How to UPDATE and REPLACE Part of a String in SQLite
In SQLite, updating and replacing parts of a string can be a common task, especially when dealing with textual data. SQLite, serverless architecture offers various methods to solve this problem. In this article, We will learn about string replace in a query with the help of various methods to know h
4 min read
How to UPDATE and REPLACE Part of a String in SQL Server
In SQLServer, efficient manipulation of strings is crucial for managing databases effectively. Among the fundamental operations are updating and replacing parts of strings within tables. These operations are invaluable for correcting data inconsistencies, enhancing data quality, and transforming tex
4 min read
How to UPDATE and REPLACE Part of a String in MariaDB
MariaDB is one of the most popular open-source database systems. It is developed by the developers of MySQL. In this article, we will How to UPDATE and REPLACE part of a string in MariaDB along with various examples and methods and so on. MariaDB REPLACE String FunctionThe REPLACE function is used t
3 min read
How to Replace Part of a String in MySQL?
To replace a part of a string in MySQL we use the REPLACE function. MySQL provides this method to manipulate string data in the tables. In this article, we are going to see how we can update a part of the string with some other value in MySQL. Understanding this can enable string manipulation in a m
4 min read
How to replace String in PHP ?
Replacing a string in PHP involves substituting parts of a string with another string. Common methods include str_replace() for simple replacements, preg_replace() for pattern-based replacements, substr_replace() for positional replacements, and str_ireplace() for case-insensitive replacements. Each
3 min read
How to Use PL SQL Insert, Update, Delete And Select Statement?
PL/SQL is a powerful extension of SQL specifically designed for Oracle databases. It enables developers to create procedural logic and execute SQL commands within Oracle database environments. In this article, we will explore the usage of fundamental PL/SQL statements such as INSERT, UPDATE, DELETE,
6 min read
How to Update Top 100 Records in PL/SQL?
In terms of database management, the ability to update specific subsets of data is crucial for maintaining system integrity and meeting user needs. In this article, we will understand two primary methods for updating top records. Using the ROWNUM function and Using the ORDER BY clause. Each method i
4 min read
How to Use str_replace in R?
str_replace() is used to replace the given string with a particular value in R Programming Language. It is available in stringr library, so we have to load this library. Syntax: str_replace( "replacing string", "replaced string") where, replacing string is the string to be replacedreplaced string is
2 min read
How to insert a line break in a String PL/SQL
In PL/SQL, inserting line breaks into VARCHAR or NVARCHAR strings can be a good way to enhance the readability and presentation of our data. Whether we are formatting output for display or preparing text for storage, knowing how to insert line breaks is a helpful skill. Here, we will explore techniq
5 min read
How to replace a portion of strings with another value in JavaScript ?
In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca
3 min read