Home Database Mysql Tutorial oracle字符/字符串替换

oracle字符/字符串替换

Jun 07, 2016 pm 05:46 PM
String replacement

在ORACLE中的字符串替换 replce、regexp_replace 和 translate函数,下面我来给大家介绍一下。

在ORACLE中的字符串替换 replce、regexp_replace 和 translate函数,下面我来给大家介绍一下。

一、语法

repalce(str_source,str1,str2)  把 str_source 中 str1 字符串替换为 str2 字符串,当 str2 为 null 或'' 时,与下个作用相同

replace(str_source,str1)         把str_source 中的 str1 字符串剔除

regexp_replace(str_source,pattern_str,rep_str) 支持正则表达式,用法类似于 replace,但功能更强大

regexp_replace(str_source,pattern_str)   把 str_source 中的 pattern_str 字符串剔除

translate(str_source,chr1,chr2) 以字符为单位,把 str_source 中的 chr1 字符对应替换为 chr2。如果 chr1 比chr2 长,那么在 chr1 中而不在 chr2 中的字符将被剔除,因为没有对应的替换字符。需注意 chr2 不能为 null 或'',否则返回值也为空


REPLACE 函数是用另外一个值来替代串中的某个值。例如,可以用一个匹配数字来替代字母的每一次出现。REPLACE 的格式如下所示:

1.REPLACE ( char, search_string [, replace_string]) 如果没有指定replace_string 变量的值,那么当发现search_string 变量的值时,就将其删除。输入可以为任何字符数据类型——CHAR、VARCHAR2、NCHAR、NVARCHAR2、CLOB或NCLOB。

--------------------------------------------------------------------------------------------------

2、REGEXP_REPLACE 函数在几个方面扩展了REPLACE 函数的功能。它支持在搜索模式中使用正则表达式,也支持本章前面描述的变量,即position、occurrence 和match_parameter,从而可以选择只替代某些匹配的值,或者不区分大小写。

 

 代码如下 复制代码

REGEXP_REPLACE( source_string, pattern 

[, replace_string 

[, position 

[, occurrence 

[, match_parameter ] 

)

除了replace_string,这里所有的变量都已经在本章前面章节作了介绍。replace_string 告诉Oracle 用什么来替代source_string 中与pattern 匹配的部分。occurrence 变量是一个非负整数,它指定操作的次数:如果为0,则所有的匹配项都被替代;如果指定一个正数,则Oracle替代第n 次匹配。

 代码如下 复制代码

1.select REGEXP_SUBSTR (Phone, 
2.'([[:digit:]]{3})-([[:digit:]]{3})-([[:digit:]]{4})' 
3.) "REGEXP_SUBSTR" 
4.from ADDRESS; 
5.REGEXP_SUBST 
6.------------ 
7.213-555-0223 
8.415-555-7530 

实例

SQL> select replace('4683,968,969',',','$') from dual;

REPLACE('4683,968,969',',','$'
------------------------------
4683$968$969

SQL> select to_number('520') from dual;

TO_NUMBER('520')
----------------
 520


------------------------------------------------------------------------------------------------------------

3、一个字符替换函数translate,不同于replace函数的是,translate函数是字符级别的替换,而不是字符串的替换。

其语法如下:

TRANSLATE ( expr , from_string , to_string )

简单的说就是对expr内容,用to_string中的字符逐一替换from_string 中的字符,举例说明如下:

 代码如下 复制代码

SQL> select translate('123456789','456','abc') from dual;

TRANSLATE
---------
123abc789

SQL> select translate('123456789','456','ab') from dual;

TRANSLAT
--------
123ab789

SQL> select translate('123456789','4564','a') from dual;

TRANSLAT
-------
123a789


select translate('abcc123a','abc','-+='),translate('abcc123a','abc','-+'),translate('abcc123a','#abc','#') from dual;

 TRANSLATE('ABCC123A','ABC','-+ TRANSLATE('ABCC123A','ABC','-+ TRANSLATE('ABCC123A','#ABC','#
------------------------------ ------------------------------ ------------------------------

-+==123-                       -+123-                         123
1、用字符'-'、'+'、'='对应替换'a','b','c'字符;

2、'abc'长度为 3,'-+'长度为 2,字符'c'没有对应的字符来替换,因此被剔除掉;

3、剔除掉字符'a'、'b'、'c',translate 有 # 的特殊用法,以 # 开头的表示所有字符

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to replace the enter key in php How to replace the enter key in php Mar 23, 2023 am 11:12 AM

In PHP program development, sometimes text content needs to be processed, including replacing the Enter key. Below we will briefly introduce how to replace the Enter key in PHP.

How to replace a string starting with something with php regular expression How to replace a string starting with something with php regular expression Mar 24, 2023 pm 02:57 PM

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

How to replace special characters in string using regular expression in PHP How to replace special characters in string using regular expression in PHP Jun 24, 2023 am 10:46 AM

With the continuous development of the Internet, PHP's application scenarios are becoming more and more extensive. In PHP development, sometimes you need to replace special characters in a string. In this case, you can use regular expressions for replacement. This article will introduce how to use regular expressions to replace special characters in strings in PHP. First, learn the basics of regular expressions. Regular expressions are a language used to describe patterns in some strings. Regular expressions include some special characters, such as ., *, +, ?, etc. These special characters have special meanings. in PH

How to replace string from left in php How to replace string from left in php Mar 23, 2023 pm 04:53 PM

PHP is a widely used scripting language that can be used to develop various web applications and websites. In these applications, strings are an integral part. In many cases, we need to perform operations such as replacing, splitting or intercepting strings. This article will explain how to replace a string from the left in PHP.

preg_replace() function in PHP: How to replace string using regular expression preg_replace() function in PHP: How to replace string using regular expression Nov 03, 2023 am 09:09 AM

preg_replace() function in PHP: How to replace a string using regular expressions, specific code examples are needed In PHP, the preg_replace() function is a very powerful and flexible function that allows us to search and replace characters using regular expressions string. Whether it is to remove specific characters from a string or replace text in a specific format, the preg_replace() function can help us achieve it easily. First, let's take a look at preg_replace()

How to replace the 11 digits after the colon in php How to replace the 11 digits after the colon in php Mar 21, 2023 pm 04:32 PM

Replacing strings is one of the most basic operations in PHP. The replacement function is crucial in data processing and string operations, and can improve program execution efficiency and performance. There are many functions for replacing strings in PHP, such as: substr_replace, str_replace, preg_replace and so on. But for specific string replacements, special replacement rules may need to be used. In this article, we will learn how to replace the 11 digits after the colon.

How to solve php failure to replace empty string How to solve php failure to replace empty string Mar 23, 2023 pm 03:51 PM

In PHP development, string replacement is a very common operation. However, sometimes when performing string replacement, you may encounter the situation of replacing an empty string, and the result is not satisfactory. This article will explore why PHP fails to replace empty strings and provide solutions.

Use the strings.Replace function to replace substrings in a string and set the number of replacements Use the strings.Replace function to replace substrings in a string and set the number of replacements Jul 25, 2023 am 08:28 AM

Use the strings.Replace function to replace substrings in a string and set the number of replacements. In the Go language, we can use the strings.Replace function to replace substrings in a string. The signature of this function is as follows: funcReplace(s,old,newstring,nint)string where s represents the original string, old represents the substring to be replaced, new represents the replaced substring, and n represents the number of times to replace. Pass below

See all articles