In Oracle translate and replace function

Work process write sql, encountered a need to replace some of the characters in the original string of characters to the required needs, found the two functions, translate () and replace (), followed by talk about the difference between them

translate function

grammar:

translate(expr, from_string, to_string)

Meaning: translate return value expr, wherein each character appears in the from_string will be replaced in the corresponding character to_string.

usage:

1.

select translate('abcdefgh','abc' ,'123') from dual;  

 Alternatively --a is 1, b is replaced with 2, c 3 is replaced

The results: 123defgh

2.

select translate('abcdefgh','abc' ,'12') from dual; 

   - If there is no corresponding character is replaced with a null, and therefore replaced c null;

The results: 12defgh

3.

select translate('abcdefgh','abc' ,'123456') from dual;

   - If the corresponding too many characters, does not affect

The results: 123defgh

4.

select translate('abcdefgh','abc' ,'') from dual;

 - If you replace the entire character null character, directly or null

The results: null

5.

select translate('abccdefgh','&abc' ,'&') from dual; 

- If you want to screen out the corresponding character, it should pass an unrelated character, but also add a character to replace the same character;

The results: defgh

6.

select translate('abcdefgh','abcc' ,'1234') from dual;

  - If the same characters corresponding to the plurality of characters, according to a first

The results: 1233defgh;

7.

select translate('0中华人民6共和国6万岁8','#0123456789' ,'#') from dual;

  - all the numbers in the original string deleted

Results: Long live the People's Republic of China

If you want to retain the digital screen out Chinese characters, the first digital filter out, and then selected characters to the original statement left digital screening

select translate('0中华人民6共和国6万岁8','#'||translate('0中华人民6共和国6万岁8','#0123456789','#'),'#') from dual;

Results: 0668


replace function

grammar:

replace(char, search_string [, replace_string])

Meaning: replace char return value, all of which will char with the same string search_string replaced replace_string string.

usage:

1.

select replace('0123456789','0','a') from dual; 

 --0 replaced by a

The results: a123456789

2.

select replace('0123456789','0','') from dual;

  --0 replaced null

Results: 123456789

3.

select replace('0123456789','0') from dual;

  - When replace_string not specified, the char with the same character search_string deleted

Results: 123456789

4.

select replace('0123456789','012',,'abcd') from dual; 

- replace the whole string, instead of a single character to replace

The results: abcd3456789


Summary: replace both translate and substitution function, except for the replace string, the whole string corresponding replacement; and translate are for a single character, but the corresponding alternative character.

We welcome comments add a comment there! ! !

发布了16 篇原创文章 · 获赞 22 · 访问量 8万+

Guess you like

Origin blog.csdn.net/c_staunch/article/details/102795249