The use of Oracle translate functions, and the difference from replace

Source: Internet
Author: User

Translate is a function to replace a character

Grammar:

Translate (char, FROM_STR,TO_STR)

where char is the string to be processed.

From_str is a set of character sets to be replaced in order, note that the character set is not a string.

To_str is the character set that corresponds from_str to be replaced in order.

For example:

Translate (' ABCD ', ' ab ', ' ab ')

After executing the above statement, ' a ' in ' ABCD ' is replaced by ' a ', ' B ' is replaced by ' B '. So back to "ABCD"

This looks very similar to the Replace function.

However, in

Replace (' ABCD ', ' ab ', ' ab ') statement, is the ' ABCD ' in the ' ab ' whole string replaced with ' AB ', you should be able to see the difference between the above translate.

As an example,

Translate (' ACBD ', ' ab ', ' AB ') will return "ACBD" because ' a ' is replaced by ' a ', ' B ' is replaced by ' B ', and the substitution is handled separately for every 1 characters.

The replace (' ACBD ', ' ab ', ' ab ') returns "ACBD" because the string ' ab ' cannot be found, so there is no replacement processing.

Here are some translate features to note.

1.

In translate (' ABCD ', ' abc ', ' A '), the character A is replaced by ' a ', and B and C are not specified for replacement, and Oracle defaults to NULL. So return to ' Ad '.

2.

Translate (' ABCD ', ' abc ', '), even a replaced character is omitted, is it because the ABC 3 characters are replaced by NULL, so return d?

The answer is wrong, and if To_str is blank (') or null, all characters will be replaced with NULL, so the expression returns NULL.

The 3.translate has many special uses,

For example, if I want to remove all the numbers in the 1 string, you can use the
 

translate (str, ' a1234567890 ', ' a ')

In this case, a (if any) of the string will be replaced with a, while the other numbers will be substituted for NULL, note that it is null instead of a space.

Or

translate (str, ' 1234567890 ', ')

Use a space to replace the above ' a ' also OK, note that from_str and to_str are beginning with a space.

And not with

translate (str, ' 1234567890 ', ') , refer to the above features 2 , NULL is returned of the AH.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.