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 或'',否则返回值也为空
(1)select replace('abcc123','abc','123'),replace('abcc123','abc') from dual;
REPLACE('ABCC123','ABC','123') REPLACE('ABCC123','ABC')
------------------------------ ------------------------
123c123 c123
(2)select regexp_replace('abcc123','abc','*'),regexp_replace('abcc123','[ac]','*'),regexp_replace('abcc123','[ac]') from dual;
REGEXP_REPLACE('ABCC123','ABC' REGEXP_REPLACE('ABCC123','[AC] REGEXP_REPLACE('ABCC123','[AC]
------------------------------ ------------------------------ ------------------------------
*c123 *b**123 b123
1、用字符串'*'替换 'abc'字符串;
2、用字符串'*'替换 'a'和'c'字符,涉及到正则表达式的用法。
(3)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 有 # 的特殊用法,以 # 开头的表示所有字符。
一、语法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
Hive
中
并无
replace
函数,只有两个类似的函数来实现字符串的替换功能
目录
regex
p_
replace
()使用
regex
p_
replace
()统计字符串
中
字符出现的个数sql
中
的
translate
()与
replace
()的对比
translate
()
replace
()
regex
p_
replace
()
语法:
regex
p_
replace
(string A,string B,string C)
返回值:string
说明:将字符串A
中
的符合Java
正则表达式
的B的部分替换为C
使用
regex
p_repla
replace
函数说明
replace
(source,str1,str2),把 source
中
str1
字符串替换
为 str2 字符串。若 str2 为
null
或空时,相当于把 str1 字符串剔除。
replace
(source,str1) ,把 source
中
的 str1 字符串剔除。
regex
p_
replace
(source,pattern_str,rep_str
/*update SQL:执行一个语句把字段
中
%替换成''*/
update web_traffic_data_raw set ubi_bounce_rate=
replace
(ubi_bounce_rate,'%','') where web_id=1811
commit;
/*先把%去掉然后除以100再转化成char类型*/
update web_traffic_data_
代码如下:create table A_TEST ( PAYOUT_ITEM_CODE VARCHAR2(30) not
null
, FORMULA_DET VARCHAR2(1000) ) create table B_TEST ( ELEMENT_ID VARCHAR2(5) not
null
, NAME VARCHAR2(41) )FORMULA_DET列里ELEMENT_ID替换成NAME 测试数据如下 代码如下: insert into a_test (PAYOUT_ITEM_CODE, FORMULA_DET) values (‘30228’, ‘({30015}+{30016}
#每天一点点,记录工作
中
实操可行#
Oracle
中
将电话号码
中
间4位数以星号*代替
select
replace
(phone,substr(phone,4,4),'****') from table_name
excel
中
将电话号码
中
间4位数以星号*代替
REPLACE
(电话号码所在单元格,4,4,"****")
(1)语法:
REPLACE
(char, search_string,
replace
ment_string)
(2)用法:
–将char
中
的字符串search_string全部转换为字符串
replace
ment_string;
–当
replace
ment_string为
null
时,相当于剔除search_string
中
的search_string字段。
(3)举例
--将name字段
中
name为李明的数据
中
的‘李’替换为‘唐’
select
replace
(name,'李','唐') f
translate
函数
格式:
translate
(string|char|varchar input, string|char|varchar from, string|char|varchar to)
--即将input出现在from
中
的
字符串替换
成to
中
的字符串
eg:select
translate
('Asher117','Asher','Viva')
>Viva117