SQL provides built-in functions for common data manipulation. Some of these built-in functions include:
- Concatenation: Put two fields, literals, or expressions together into a single field.
- Padding: Add a character, such as a dash or a blank, to the beginning or end of a field to make it longer.
- Trimming: Pull out a portion of the data from a column, such as the first ten characters from a 30-character field.
SQL functions that operate on character fields (also called strings) are useful for many tasks, such as:
- Formatting data for output in reports, form letters, or address labels where the database column data is not stored exactly the way you want the output to appear
- Comparing a portion of the data in a column to an expression or literal; comparisons can be part of a
WHERE
clause of a query, or part of an IF
statement in PL/SQL
- Converting data to a different format
- Replacing one character of the data with a different character, such as replacing a blank space with a hyphen
The table below lists all the character functions available in Oracle and what they do.
REPLACE returns char with every occurrence of search_string replaced with replacement_string.
If replacement_string is omitted or null, then all occurrences of search_string are removed.
If search_string is null, then char is returned. Both search_string and replacement_string, as well as char, can be any of the datatypes
- CHAR,
- VARCHAR2,
- NCHAR,
- NVARCHAR2,
- CLOB, or
- NCLOB.
The string returned is in the same character set as char. The function returns VARCHAR2 if the first argument is not a LOB and returns CLOB if the first argument is a LOB. REPLACE provides functionality related to that provided by the TRANSLATE function. TRANSLATE provides single-character, one-to-one substitution. REPLACE lets you substitute one string for another as well as to remove character strings
Example: The following example replaces occurrences of J with BL:
SELECT REPLACE('JACK and JUE','J','BL') "Changes"
FROM DUAL;
Changes
--------------
BLACK and BLUE
In the next lesson, you will learn the use of the character functions CONCAT, SUBSTR, LENGTH, and INSTR.
Question: Is the "Oracle REPLACE function" unique to PL/SQL?
No, the `REPLACE` function is not unique to PL/SQL. It is a standard SQL function that is available in most SQL databases, including Oracle, MySQL, PostgreSQL, and Microsoft SQL Server. The specific syntax and behavior of the function may vary slightly between different databases, but the basic functionality of replacing occurrences of a substring with another substring is generally the same.
Here are some examples of how the `REPLACE` function is used in different databases:
Oracle
SELECT REPLACE('Hello, world!', 'world', 'universe') FROM DUAL;
MySQL
SELECT REPLACE('Hello, world!', 'world', 'universe') FROM DUAL;
PostgreSQL
SELECT REPLACE('Hello, world!', 'world', 'universe');
Microsoft SQL Server
SELECT REPLACE('Hello, world!', 'world', 'universe');
As you can see, the basic structure of the `REPLACE` function is similar across all of these databases.
The first argument is the string to be searched, the second argument is the substring to be replaced, and the third argument is the replacement string.