TRANSLATE

TRANSLATE(x,from_string, to_string) converts all occurrences of from_string in x to to_string letter by letter.


SQL> select translate('123456', '123', '456') from dual;

TRANSL
------
456456

SQL> select translate('asdfasdf123', '2ec', '3it') from dual;

TRANSLATE('
-----------
asdfasdf133

SQL>

Use TRANSLATE() to convert numbers.


SQL> SELECT TRANSLATE(12345,54321,67890) FROM dual;

TRANS
-----
09876

SQL>

Encoding a string:


SQL> SELECT TRANSLATE('THIS iS a TEST',
  2  'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  3  'EFGHIJKLMNOPQRSTUVWXYZABCD')
  4  FROM dual;

TRANSLATE('THI
--------------
XLMW iW a XIWX

SQL>

Pass column values to TRANSLATE()


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, TRANSLATE(ename,
  2  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
  3  'EFGHIJKLMNOPQRSTUVWXYZABCDefghijklmnopqrstuvwxyzabcd')
  4  FROM emp;

ENAME      TRANSLATE(
---------- ----------
SMITH      WQMXL
ALLEN      EPPIR
WARD       AEVH
JONES      NSRIW
MARTIN     QEVXMR
BLAKE      FPEOI
CLARK      GPEVO
SCOTT      WGSXX
KING       OMRK
TURNER     XYVRIV
ADAMS      EHEQW

11 rows selected.

SQL>
SQL>
SQL>
SQL>
Home »
Oracle »
Conversion Functions » 

Related: