REPLACE function

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)

SQL> SELECT REPLACE('Mississippi', 'pi', 'pI') FROM dual ;

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>

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));

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>
Home »
Oracle »
String Functions » 

Related: