Oracle 使用sql查询的sql函数在字符串中添加和删除空格

Oracle 使用sql查询的sql函数在字符串中添加和删除空格,oracle,Oracle,我有一个要求。有一个名称列/字段,它包含类似“a”的名称。aaron,我想删除圆点之前的空格,并在圆点出现后添加空格。我还想离开,如果点后面已经有空间了。我尝试用replace先删除空格,之后我不知道如何在点出现后添加空格。有人能帮我吗?1使用“替换”删除dot之前的空格 replace(column,' .','.') 2使用“替换”删除圆点后的空格 replace(column,'. ','.') 3.在点后加空格 replace(column,'.','. ') 这意味着: repl

我有一个要求。有一个名称列/字段,它包含类似“a”的名称。aaron,我想删除圆点之前的空格,并在圆点出现后添加空格。我还想离开,如果点后面已经有空间了。我尝试用replace先删除空格,之后我不知道如何在点出现后添加空格。有人能帮我吗?

1使用“替换”删除dot之前的空格

replace(column,' .','.')
2使用“替换”删除圆点后的空格

replace(column,'. ','.')
3.在点后加空格

replace(column,'.','. ')
这意味着:

replace(replace(replace(column,' .','.'),'. ','.'),'.','. ')

那不是更干净吗?我不确定需要什么样的输入数据。它是否总是有一个空格点空格子字符串或其中任何一个是可选的?在这种情况下,始终可以使用regexp\u replace

select replace('A . aaron',' . ','. ') from dual;

您也可以这样使用:

select regexp_replace(replace('A . aaron.xsd',' .','.'),'\.(\s)?','. ') from dual;
或者这个(这会在点之前和之后删除额外的空格):


它可以在一个
regexp\u replace
调用中完成。逻辑是围绕字符串的所有组件分组,然后按照您想要的顺序将这些组放在一起。尝试数据可能要测试的所有场景

SQL> with tbl(name) as (
  2  select 'A . aaron' from dual
  3  union
  4  select 'A. aaron' from dual
  5  union
  6  select 'A .aaron' from dual
  7  union
  8  select 'A.aaron' from dual
  9  )
 10  select name, regexp_replace(name, '^(\w)( )?(\.)( )?(.*)', '\1\3 \5') fixed_name
 11  from tbl;

NAME      FIXED_NAME
--------- ---------------
A . aaron A. aaron
A .aaron  A. aaron
A. aaron  A. aaron
A.aaron   A. aaron

SQL>
匹配模式解释如下:

^     Match the beginning of the string
(     Start first remembered group
\w    Match a word. matches up to a space or punctuation character
)     End first remembered group
( )?  Followed by the second group which is an optional space
(\.)  Followed by a literal period (third group)
( )?  Followed by the 4th group which is an optional space
(.*)  Followed by the 5th remembered group, the rest of the string.
\1      Replace with the first remembered group
\3      Followed by the 3rd remembered group which should be the literal period
<space> Followed by a space
\5      Followed by the rest of the string
替换模式说明:

^     Match the beginning of the string
(     Start first remembered group
\w    Match a word. matches up to a space or punctuation character
)     End first remembered group
( )?  Followed by the second group which is an optional space
(\.)  Followed by a literal period (third group)
( )?  Followed by the 4th group which is an optional space
(.*)  Followed by the 5th remembered group, the rest of the string.
\1      Replace with the first remembered group
\3      Followed by the 3rd remembered group which should be the literal period
<space> Followed by a space
\5      Followed by the rest of the string

有趣的是,需要对匹配正则表达式中的句点进行转义(否则它是一个特殊的正则表达式符号,表示任何字符),而在替换字符串中,它不是,因为它是一个文本句点。

能否显示一些示例数据、预期输出以及您迄今为止尝试过的代码,谢谢选择替换('a.aaron','')来自双重;我用它来删除eror,输出如下:A.aaron。但我的预期输出必须像A.aaron。在未来,请包括显示所有可能条件的“之前”数据样本,以及“之后”数据,即给定所有这些条件的所需输出。