【问题标题】:How to remove the blank spaces between two lines in a column如何删除列中两行之间的空格
【发布时间】:2022-01-20 20:45:22
【问题描述】:

我有一列返回以下输出:

PERSON_NUMBER                                    FEEDBACK
13636                                         -Very attentive during our sessions
                                            
                                              -Very interative session, questions 
                                                were asked and answered well.
                                            


                                              Nice turn over 

                                              Debates were good

我希望 FEEDBACK Column 的输出看起来像:

PERSON_NUMBER                                    FEEDBACK
13636                                         -Very attentive during our sessions                                           
                                              -Very interative session, questions 
                                                were asked and answered well.
                                              Nice turn over 
                                              Debates were good

即应该删除行之间的多余空格。

【问题讨论】:

  • 那些看起来像空白行 - 你有什么理由称它们为“空格”?空格是同一行的两个单词之间的一个空格。

标签: sql oracle


【解决方案1】:

您可以在 (?:\r?\n){2,} 上进行正则表达式替换,然后只替换为一个 CR?LF:

SELECT PERSON_NUMBER,
       REGEXP_REPLACE(FEEDBACK,
                      '(' || chr(13) || '?' || chr(10) || '){2,}',
                      chr(13) || chr(10)) AS FEEDBACK
FROM yourTable;

【讨论】:

  • 我使用了完全相同的查询,但我在输出中得到

    ,

     

    .....无论如何我可以删除它?
【解决方案2】:

如果您需要删除所有看起来为空的行(例如,还可能包含空格、制表符或其他不可打印的字符),那么您可以使用[:graph:] 类的否定正则表达式查找此类行并将其替换为空行,然后将所有换行重复替换为单个。

下面是代码:

with a as (
  select q'[-Very attentive during our sessions
                                            
-Very interative session, questions 
 were asked and answered well.]' || chr(13) || chr(10) || q'[
                                            
       ]' || chr(10) || q'[      

]' || chr(10) || chr(10) || q'[Nice turn over]' as q
from dual
)
select
  regexp_replace(
    regexp_replace(
      q,
      /*
        Replace LF followed by any non-printable sequence that ends with newline
        with single newline
      */
      chr(10) || '[^[:graph:]]*(' || chr(13) || '?' || chr(10) || ')',
      chr(10) || '\1'
    ),
    /*Then replace newline repetitions*/
    '(' || chr(13) || '?' || chr(10) || ')+',
    '\1'
  ) as q
from a
q
-Very attentive during our sessions
-Very interative session, questions
 were asked and answered well.
Nice turn over

db小提琴here

【讨论】:

  • 我使用了完全相同的查询,但我在输出中得到

    ,

     

    .....无论如何我可以删除它?
  • 对不起,我不知道你在说什么。查询的输入是什么?您可以通过在链接的小提琴中运行上述代码来检查它的输出。