需求
有一个字段还有子母呼和数字,现要将字母和数字进行分离;
portyarn8088manager porthdfs9820filse porthive10000query
要求输出结果
portyarn8088manager portyarnmanager 8088 porthdfs9820filse porthdfsfilse 9820 porthive10000query porthivequery 10000
select 'porthdfs9820filse' port_info union select 'porthive10000query' port_info union select 'portyarn8088manager' port_info
思路
解题在于把不同字符替或数字替换成一个指定的特征字符或特征数字,这样可以将一个字符就替换所有的字符,一个特征数字就可以代表所有的数字,在删除数据的时候,无需逐一查找多个字符或多个数字了。1、将特征字段替换成指定字符
select port_info, translate(lower(port_info), 'abcdefghijklmnopqrstuvwxyz', repeat('#', 26)) temp_info_1, translate(lower(port_info), '0123456789', repeat('&', 10)) temp_info_2 from (select 'porthdfs9820filse' port_info union select 'porthive10000query' port_info union select 'portyarn8088manager' port_info) t;.
结果如下
porthdfs9820filse ########9820##### porthdfs&&&&filse porthive10000query ########10000##### porthive&&&&&query portyarn8088manager ########8088####### portyarn&&&&manager2、通过正则,将特定字符删除
select port_info, regexp_replace(temp_info_2, '\\&', '') alb, regexp_replace(temp_info_1, '\\#', '') zm from (select port_info, translate(lower(port_info), 'abcdefghijklmnopqrstuvwxyz', repeat('#', 26)) temp_info_1, translate(lower(port_info), '0123456789', repeat('&', 10)) temp_info_2 from (select 'porthdfs9820' port_info union select 'porthive10000' port_info union select 'portyarn8088' port_info) t) t;
结果
porthdfs9820 porthdfs 9820 porthive10000 porthive 10000 portyarn8088 portyarn 8088