需要按空格替换字母数字字符并创建新列

时间:2017-09-14 14:59:35

标签: sql oracle

我需要转换以下内容

Column 1          Column 2
ABC, Company   ABC Company
TA. Comp       TA Comp

如何在sql中获取Column2,我将删除所有',' ''太空了。

3 个答案:

答案 0 :(得分:1)

怎么样:

with testdata as (
  select 'ABC, Company Inc.' as col1 from dual
  union all
  select 'TA. Comp' as col1 from dual
)
select trim(regexp_replace(regexp_replace(col1, '[[:punct:]]',' '), ' {2,}', ' ')) as col2
from testdata;

输出:

ABC Company Inc
TA Comp

假设标点符号是您尝试删除的内容。

答案 1 :(得分:0)

您可以尝试使用:

SELECT REGEXP_REPLACE(column1, '[^a-zA-Z0-9 ]+', '')
FROM DUAL

答案 2 :(得分:0)

with t (val) as 
(
  select 'ABC,. Cmpany' from dual union all 
  select 'A, VC' from dual union all
  select 'A,, BC...com' from dual
)
select 
  val, 
  replace(replace(val, ',', ''), '.', '') x , -- one way
  regexp_replace(val, '[,.]', '') y           -- another way
from t
;

VAL             X          Y        
--------------- ---------- ----------
ABC,. Cmpany    ABC Cmpany ABC Cmpany
A, VC           A VC       A VC      
A,, BC...com    A BCcom    A BCcom