Oracle-函数-替换字符串

    在平常的工作当中,经常需要替换字符串中的字符,比如将身份证号1991年的全部替换为2001年,替换的方式有三种(TRANSLATE、REPLACE、REGEXP_REPLACE),支持嵌套调用:

/*

 REPLACE(char,search_string,replacement_string)

‘1991‘ is search_string
‘2001‘ is replacement_string
REPLACE returns char with every occurrence of search_string replaced with replacement_string. 
If replacement_string is omitted or null,then all occurrences of search_string are removed.
If search_string is null,then char is returned. REPLACE的实现是由TRANSLATE函数为基础,在REPLACE中,原值和被替换项与替换后的项数据类型支持:CHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB,or NCLOB
*/ SELECT REPLACE(2301231991,1991,2001) FROM DUAL;
执行结果为:‘2301232001‘

 TRANSLATE(char,from_string,to_string)

--‘ */‘‘‘ is from_strin --‘_‘ is to_string --被替换项从左至右依次替换,替换项按照使用个数递减,若替换项不足则替换为‘‘
SELECT TRANSLATE(SQL*Plus User‘‘s Guide, */‘‘‘,_) FROM DUAL; --执行结果为:SQLPlus_Users_Guide
SELECT TRANSLATE(SQL*Plus User‘‘s Guide,__) FROM DUAL; --执行结果为:SQL_Plus_Users_Guide
SELECT TRANSLATE(SQL*Plus User‘‘s Guide,___) FROM DUAL; --执行结果为:SQL_Plus_Users_Guide
SELECT TRANSLATE(SQL*Plus User‘‘s Guide,____) FROM DUAL; --执行结果为:SQL_Plus_User_s_Guide
 
 

 REGEXP_REPLACE(source_char,match_parameter,replace_string)

--REGEXP_REPLACE的实现是由REPLACE函数为基础,支持正则表达式,可以进行复杂字符处理
SELECT REGEXP_REPLACE(132.546.7890,([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4}),(\1) \2-\3)  FROM DUAL ;
--The following example examines phone_number,looking for the pattern xxx.xxx.xxxx. Oracle reformats this pattern with (xxx) xxx-xxxx.
--执行结果为:(132) 546-7890
SELECT REGEXP_REPLACE(500   Oracle     Parkway,Redwood  Shores,CA,( ){2,}, ) FROM DUAL;
--The following example examines the string,looking for two or more spaces. Oracle replaces each occurrence of two or more spaces with a single space.
--执行结果为:500 Oracle Parkway,Redwood Shores,CA

相关文章

文章浏览阅读773次,点赞6次,收藏9次。【代码】c# json字符...
文章浏览阅读8.7k次,点赞2次,收藏17次。此现象一般定位到远...
文章浏览阅读2.8k次。mysql脚本转化为oracle脚本_mysql建表语...
文章浏览阅读2.2k次。cx_Oracle报错:cx_Oracle DatabaseErr...
文章浏览阅读1.1k次,点赞38次,收藏35次。本文深入探讨了Or...
文章浏览阅读1.5k次。默认自动收集统计信息的时间为晚上10点...