Differences between translate and replace in SQL

Source: Internet
Author: User
Tags translate function
1. translate Syntax: TRANSLATE (char, from, to) usage: returns a string that replaces each character that appears in from with the corresponding character in. If from is longer than to string, the extra characters in from are deleted. One of the three parameters is null, And the return value is also null. Example: SQLselecttransla

1. translate Syntax: TRANSLATE (char, from, to) usage: returns a string that replaces each character that appears in from with the corresponding character in. If from is longer than to string, the extra characters in from are deleted. One of the three parameters is null, And the return value is also null. Example: SQL select transla

1. translate Syntax: TRANSLATE (char, from, to) usage: returns a string that replaces each character that appears in from with the corresponding character in. If from is longer than to string, the extra characters in from are deleted. One of the three parameters is null, And the return value is also null. Example: SQL> select translate ('abcdefga ', 'abc', 'wo') return value from dual; Return Value ------- wodefgw analysis: this statement converts 'abc' in 'abcdefga 'to 'wo'. Because 'A' in 'abc' corresponds to 'w' in 'Wo ', therefore, 'A' in 'abcdefga 'is converted to 'w', while' B 'in 'abc' corresponds to 'O' in 'Wo ', therefore, 'B' in 'abcdefga 'is converted to 'O'. 'C' in 'abc' does not have the corresponding character in 'Wo, therefore, you can delete all 'C' in 'abcdefga '. Simply put, you can convert the character from to the character corresponding to its position, if the corresponding character cannot be found in to, the character in the returned value will be deleted. In actual business, it can be used to delete some abnormal data. For example, the t_no field in Table a indicates the phone number, and the phone number itself should be a string composed of numbers, to delete abnormal data that contains non-numbers, the translate function is used: SQL> delete from a, where length (translate (trim (. t_no), '000000' |. t_no, '200') <> length (trim (. t_no); 2. replace Syntax: REPLACE (char, search_string, replacement_string) usage: converts all string search_string in char to string replacement_string. Example: SQL> select REPLACE ('fgsgswsgs ', 'fk', 'J') return value from dual; Return Value --------- fgsgswsgs SQL> select REPLACE ('fgsgswsgs ', 'sg ', 'eeerrrttt') return value from dual; Return Value ------------------- analyze: In the first example, because 'fgsgswsgs 'does not have a string that matches 'fk', the return value is still 'fgsgswsgs '; in the second example, convert all the strings 'sg 'in 'fgsgswsgs' to 'eeerrrttt '. Conclusion: replace and translate are both substitution functions,

However, replace is for strings, while translate is for a single character.

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.