Oracle String/Char Function - Oracle/PLSQL REPLACE Function






This Oracle tutorial explains how to use the Oracle/PLSQL REPLACE function.

The Oracle/PLSQL REPLACE function replaces a sequence of characters in a string with another group of characters.

This function replaces every occurrence of the pattern-to-find with pattern-to-replace-by.

The general format for this function is:

REPLACE(source-string, pattern-to-find, pattern-to-replace-by)

Syntax

The syntax for the Oracle/PLSQL REPLACE function is:

REPLACE( string1, string_to_replace, [ replacement_string ] )

string1 is the string to be replaced.

string_to_replace is the string that will be searched for in string1.

replacement_string is optional. All occurrences of string_to_replace will be replaced with replacement_string in string1. If the replacement_string is omitted, REPLACE removes all occurrences of string_to_replace.





Example


SQL> SELECT REPLACE('Mississippi', 'pi', 'pI') FROM dual ;
-- ww w  .  ja  v  a 2s  .c o  m
REPLACE('MI
-----------
MississippI

SQL> SELECT REPLACE ('This is a test',' is ',' are ') FROM dual;

REPLACE('THISIS
---------------
This are a test

SQL>

Replacement may happen more than once.


SQL> SELECT REPLACE ('This is a test','is',' are ') FROM dual;

REPLACE('THISISATEST
--------------------
Th are   are  a test

SQL>

If the pattern-to-find is not present, then the replacing does not occur:


SQL> SELECT REPLACE ('This is a test','are',' are not ') FROM dual;

REPLACE('THISI
--------------
This is a test

SQL>




Example 2

REPLACE() doesn't modify the actual value, only the row returned by the function is modified.


CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
                  ENAME VARCHAR2(10),
                  JOB VARCHAR2(9),
                  SAL NUMBER(7, 2),
                  DEPTNO NUMBER(2));
-- from w w  w  .ja v  a 2  s . com
INSERT INTO EMP VALUES (1, 'SMITH', 'CLERK',     800,    20);
INSERT INTO EMP VALUES (2, 'ALLEN', 'SALESMAN', 1600,    30);
INSERT INTO EMP VALUES (3, 'WARD',  'SALESMAN', 1250,    30);
INSERT INTO EMP VALUES (4, 'JONES', 'MANAGER',  2975,    20);
INSERT INTO EMP VALUES (5, 'MARTIN','SALESMAN', 1250,    30);
INSERT INTO EMP VALUES (6, 'BLAKE', 'MANAGER',  2850,    30);
INSERT INTO EMP VALUES (7, 'CLARK', 'MANAGER',  2850,    10);
INSERT INTO EMP VALUES (8, 'SCOTT', 'ANALYST',  3000,    20);
INSERT INTO EMP VALUES (9, 'KING',  'PRESIDENT',3000,    10);
INSERT INTO EMP VALUES (10,'TURNER','SALESMAN', 1500,    30);
INSERT INTO EMP VALUES (11,'ADAMS', 'CLERK',    1500,    20);

SQL> select ename, replace(ename,'SMITH','AAAAA') from emp;

ENAME      REPLACE(ENAME,'SMITH','AAAAA')
---------- --------------------------------------------------
SMITH      AAAAA
ALLEN      ALLEN
WARD       WARD
JONES      JONES
MARTIN     MARTIN
BLAKE      BLAKE
CLARK      CLARK
SCOTT      SCOTT
KING       KING
TURNER     TURNER
ADAMS      ADAMS

11 rows selected.

SQL>
SQL> select ename from emp;

ENAME
----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS

11 rows selected.

SQL>