Explanation of the use of Translate in Oracle and DB2 databases, db2translate

Source: Internet
Author: User

Explanation of the use of Translate in Oracle and DB2 databases, db2translate

I. Oracle Syntax:

Translate (string, from_str, to_str)

Eg: select translate ('abcdef', 'abf', 'cde') from dual;

Result: Move 'abf' in the 'abcdef' string to 'cde ')

2. Syntax in DB2:

Translate (string, to_str, from_str)

Eg: select translate ('abcdef', 'abf', 'cde') from sysibm. sysdummy1;

Result: ababff (replace 'cde' in the 'abcdef' string with 'abf ')

String: string to be processed

From_str: string character to be converted

To_str: character to be converted

(Note: In Oracle and DB2, the positions of the from_str and to_str parameters in the Translate method are exactly the opposite)

The Translate function searches for characters in from_str in string and replaces them with the characters in to_str (single character replacement ).

Iii. Use Cases of the Translate function (taking DB2 as an example ):

1. Verify a field (mobile phone number, zip code, date ...) Whether to include characters except numbers:

Eg: select trim (translate ('172.16345489asx', '', '123') from sysibm. sysdummy1;

(Trim is used to remove spaces)

Result: asx

2. Convert the number of a field to 9 and the letter to X:

Eg: select translate ('xgz201601 ', '9... X... ', '0123456789abcdef... ') From sysibm. sysdummy1;

(The intermediate parameters are 10 9 and 26 X, followed by 0-9 and A-Z)

Result: XXX999999

3. Extract letters or numbers from a string:

Select translate ('xgz201601 ', '', 'abcdef... ') From sysibm. sysdummy1;

Result: 201601

4. Conclusion:

1. If the character to be converted (from_str) does not exist in the character to be converted (to_str), the conversion is truncated:

Eg: select translate ('abcde', '12', 'bcde') from sysibm. sysdummy1;

Result: a12

2. The destination string (to_str) to be converted in oracle cannot be '', because'' is considered as a null value in oracle, so it cannot be matched and is returned as a null value. However, matching can be performed in DB2:

Eg :( oracle) select translate ('abcde', 'abc', '') from dual;

Display result:

Eg :( DB2) select translate ('abcde', '', 'abc') from sysibm. sysdummy1;

Display result: de

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.