分离特殊字符并显示它们

时间:2017-02-14 06:49:25

标签: sql oracle

我有一个列名为Table的表,如下所示:

Name
ftyrt_hghjh
hgdhfg_jhjh
dfgfh#hvhvj
vhhghf=dvdf
hdghfg%bdjfb

我想分隔特殊字符并显示如下:

Name             Special_Char
ftyrt_hghjh          _    
hgdhfg_jhjh          _
dfgfh#hvhvj          #
vhhghf=dvdf          =
hdghfg%bdjfb         %

2 个答案:

答案 0 :(得分:1)

with temp as 
(select 'ftyrt_hghjh' as name from dual
 union all
select 'hgdhfg_jhjh' as name from dual
 union all
select 'dfgfh#hvhvj' as name from dual
 union all
select 'vhhghf=dvdf' as name from dual
 union all
select 'hdghfg%bdjfb' as name from dual
)
select name AS "Name"
      ,regexp_substr(name, '[^a-zA-Z]') AS "Special Character"
from temp

使用regexp匹配a-zA-Z以外的任何字符 Match Special Character

答案 1 :(得分:0)

您可以使用函数translate来替换所有非特殊字符,实际上是从输入字符串中删除它们。

假设特殊字符是

  

_#=%

并假设非特殊字符从a到z都是小写的 以下内容将起作用:

translate (table.name, '_#=%abcdefghijklmnopqrstuvwxyz', '_#=%')