How to Replace String Using Regexp_replace In Oracle?

9 minutes read

To replace a string using REGEXP_REPLACE in Oracle, you need to specify the input string, the regular expression pattern to match, the replacement string, and any optional flags for the regex operation.


For example, if you want to replace all occurrences of the word "apple" with "orange" in a column named "fruit" in a table named "fruits_table", you can use the following SQL query:

1
2
UPDATE fruits_table
SET fruit = REGEXP_REPLACE(fruit, 'apple', 'orange', 1, 0, 'i')


In this query:

  • The first argument 'fruit' is the input string column in which the replacement will be performed.
  • The second argument 'apple' is the regular expression pattern to match.
  • The third argument 'orange' is the replacement string.
  • The fourth argument '1' is the starting position in the string to begin replacing (1 means start from the beginning of the string).
  • The fifth argument '0' is the number of occurrences to replace. 0 means replace all occurrences.
  • The sixth argument 'i' is a flag for case-insensitive matching.


After running this query, all instances of the word "apple" in the "fruit" column will be replaced with "orange".

Best Oracle Database Books of November 2024

1
OCA Oracle Database SQL Exam Guide (Exam 1Z0-071) (Oracle Press)

Rating is 5 out of 5

OCA Oracle Database SQL Exam Guide (Exam 1Z0-071) (Oracle Press)

2
Oracle PL/SQL Programming: Covers Versions Through Oracle Database 12c

Rating is 4.9 out of 5

Oracle PL/SQL Programming: Covers Versions Through Oracle Database 12c

  • O Reilly Media
3
Oracle Database 12c PL/SQL Programming

Rating is 4.8 out of 5

Oracle Database 12c PL/SQL Programming

4
Beginning Oracle Database 12c Administration: From Novice to Professional

Rating is 4.7 out of 5

Beginning Oracle Database 12c Administration: From Novice to Professional

5
Expert Oracle Database Architecture: Techniques and Solutions for High Performance and Productivity

Rating is 4.6 out of 5

Expert Oracle Database Architecture: Techniques and Solutions for High Performance and Productivity

6
Expert Oracle Database Architecture

Rating is 4.5 out of 5

Expert Oracle Database Architecture

  • Apress
7
Oracle Database Application Security: With Oracle Internet Directory, Oracle Access Manager, and Oracle Identity Manager

Rating is 4.4 out of 5

Oracle Database Application Security: With Oracle Internet Directory, Oracle Access Manager, and Oracle Identity Manager

8
Oracle Database 12c PL/SQL Advanced Programming Techniques

Rating is 4.3 out of 5

Oracle Database 12c PL/SQL Advanced Programming Techniques

9
Oracle Database 11g SQL (Oracle Press)

Rating is 4.2 out of 5

Oracle Database 11g SQL (Oracle Press)

10
Oracle 12c For Dummies

Rating is 4.1 out of 5

Oracle 12c For Dummies


How to test the REGEXP_REPLACE function in Oracle before applying it to a full dataset?

One way to test the REGEXP_REPLACE function in Oracle before applying it to a full dataset is to use dummy data and run the function on a small sample set. This will allow you to verify that the function works as expected and produces the desired results.


Here is an example of how you can test the REGEXP_REPLACE function in Oracle:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
-- Create a sample table with dummy data
CREATE TABLE test_data (
    id NUMBER,
    text VARCHAR2(100)
);

INSERT INTO test_data VALUES (1, '123-456-7890');
INSERT INTO test_data VALUES (2, 'abc-def-ghi');

-- Run the REGEXP_REPLACE function on the sample data
SELECT id, text, REGEXP_REPLACE(text, '[0-9]+', 'X') AS replaced_text
FROM test_data;


In this example, the REGEXP_REPLACE function is used to replace all numerical digits in the 'text' column with the letter 'X'. By running this query on a small sample set, you can verify that the function works correctly and produces the desired output before applying it to a larger dataset.


How to escape special characters in the search pattern of REGEXP_REPLACE in Oracle?

To escape special characters in the search pattern of REGEXP_REPLACE in Oracle, you can use the backslash () character. Here is an example:

1
SELECT REGEXP_REPLACE('Hello@World!', '\@', '') FROM dual;


In this example, the backslash () is used to escape the special character "@" in the search pattern. The REGEXP_REPLACE function will replace the "@" character with an empty string, resulting in the output "HelloWorld!".


How to use boundary characters in REGEXP_REPLACE in Oracle?

Boundary characters in regex represent the beginning or end of a word or line. Here is an example of how to use boundary characters in REGEXP_REPLACE in Oracle:

  1. Using ^ character to represent the start of a word:
1
2
SELECT REGEXP_REPLACE('Hello World', '^\w+', 'Hi') AS replaced_text
FROM dual;


This will replace the first word 'Hello' with 'Hi'.

  1. Using $ character to represent the end of a word:
1
2
SELECT REGEXP_REPLACE('Hello World', '\w+$', 'Universe') AS replaced_text
FROM dual;


This will replace the last word 'World' with 'Universe'.

  1. Using \b characters to represent word boundary:
1
2
SELECT REGEXP_REPLACE('Hello World', '\b\w{5}\b', 'Hi') AS replaced_text
FROM dual;


This will replace any 5-letter word in the string with 'Hi'.


These are just a few examples of how to use boundary characters in REGEXP_REPLACE in Oracle. You can combine boundary characters with other regex patterns to create more complex replacement rules.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To remove all characters except 'e' in Oracle, you can use the REGEXP_REPLACE function. Here is an example query: SELECT REGEXP_REPLACE('Hello world!', '[^e]', '') AS result FROM dual; In this query, the REGEXP_REPLACE function ...
To remove special characters from a string in PostgreSQL, you can use the regexp_replace function. This function allows you to replace a pattern in a string with a specified string. You can use a regular expression pattern to identify and replace special chara...
In Oracle, to escape a regexp_replace function, you can use the backslash () character before any special characters that are part of the regular expression syntax. This will treat the special characters as literal characters. For example, if you want to repla...