简体   繁体   中英

Avoid nested regexp_replace( ) for changing multiple char(s) in a string

I want to replace multiple char(s) in a string using a simple query for Oracle 11g. Now I use:

select regexp_replace(regexp_replace(regexp_replace(colName, 'a', 'x'), 'b', 'y' ) 'c', 'z') from myTable;

but I'm not very happy with it. What would be a more intuitive query that has the same output?

translate() searches the string in the first argument for characters in the second argument and replaces them with the characters found in the third argument that are in the same position as those in the second argument:

SQL> select translate('abc', 'abc', 'xyz')
     from dual;

TRA
---
xyz

SQL> select translate('tralalajustbecause','abc','xyz') from dual;

TRA
---
trxlxlxjustyezxuse 

翻译功能将完成您​​想要的工作:

select translate(colName, 'abc', 'xyz') from myTable;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM