ITPub博客

首页 > 数据库 > Oracle > translate与replace

translate与replace

原创 Oracle 作者:hanjs 时间:2007-02-03 10:32:49 0 删除 编辑

Oracle/PLSQL: Translate Function

In Oracle/PLSQL, the translate function replaces a sequence of characters in a string with
another set of characters. However, it replaces a single character at a time. For example,
it will replace the 1st character in the string_to_replace with the 1st character in the replacement_string.
Then it will replace the 2nd character in the string_to_replace with the 2nd character
in the replacement_string, and so on.

The syntax for the translate function is:

translate( string1, string_to_replace, replacement_string )

string1 is the string to replace a sequence of characters with another set of characters.

string_to_replace is the string that will be searched for in string1.

replacement_string - All characters in the string_to_replace will be replaced with the
corresponding character in the replacement_string.

For example:

translate('1tech23', '123', '456); would return '4tech56'
translate('222tech', '2ec', '3it'); would return '333tith'

Oracle/PLSQL: Replace Function

In Oracle/PLSQL, the replace function replaces a sequence of characters in a string with
another set of characters.

The syntax for the replace function is:

replace( string1, string_to_replace, [ replacement_string ] )

string1 is the string to replace a sequence of characters with another set of characters.

string_to_replace is the string that will be searched for in string1.

replacement_string is optional. All occurrences of string_to_replace will be replaced with
replacement_string in string1. If the replacement_string parameter is omitted,
the replace function simply removes all occurrences of string_to_replace, and returns the resulting string.

For example:

replace('123123tech', '123'); would return 'tech'
replace('123tech123', '123'); would return 'tech'
replace('222tech, '2', '3'); would return '333tech'
replace('0000123', '0'); would return '123'
replace('0000123', '0', ' '); would return ' 123'

Note: Translate and replace are very similar in their appearance but can produce very different results. Translate replaces by position, the first character of the list to match is replaced by the first character of the replacement list. The second character with the second, and if there are characters in the list to match that do not have positional equivalents in the replacements list they are dropped.

Replace replaces the string to match with the replacement string. The replacement of a single character is the same as that of TRANSLATE.

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7659204/viewspace-897096/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-01-09

  • 博文量
    23
  • 访问量
    83802