【问题标题】:Dividing string and move down the rest of it分割字符串并向下移动其余部分
【发布时间】:2018-07-18 14:04:15
【问题描述】:

有人可以帮助我吗?我有一个字符串:

This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example.

我想删掉其中的 35 个字符,其余的字符串移到最后一行。之后,我想在第二行剪切 35 个字符的字符串,其余的字符串移到第三行。之后,我想在第三行剪切 35 个字符的字符串并将其余字符串移动到第四行。它应该是这样的:

This is example. This is example. T
his is example. This is example. Th
is is example. This is example. Thi
s is example. This is example. This
is example.

我试图用 substring 函数做一些事情,但我不知道如何向下移动我的字符串的其余部分。

【问题讨论】:

  • 您需要将字符串拆分为更多记录还是需要通过换行来编辑字符串?
  • 我只想通过添加换行符来编辑字符串。

标签: sql oracle split


【解决方案1】:

您可以使用正则表达式在 35 个字符之后添加行分隔符,或者您需要的任何内容;例如:

select regexp_replace(str, '(.{35})', '\1' || chr(13))
FROM (
        SELECT 'This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example.' STR
        FROM DUAL
     )

这只是获取一组 35 个字符 ('(.{35})') 并将每个组替换为自身加上行分隔符 ('\1' || chr(13))

【讨论】:

    【解决方案2】:

    嗯,可能这不是你要找的,但你可以写这个函数:

    create or replace function split_n(n in number, s in varchar2) return varchar2
      is
         t varchar2(1024);
         ret varchar2(4096);
         rem varchar2(4096);
      begin
        rem := s;
        loop
          t := substr(rem, 1, n);
          ret := ret || t || chr(10);
          rem := substr(rem, n+1);
          exit when rem is null;
        end loop;
        return ret;
      end split_n;
    

    然后你只需使用:

    > select split_n(35, 'This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example.') from dual
    

    【讨论】:

      【解决方案3】:

      这次不需要大解(Splitting string into multiple rows in Oracle):

         with temp as
      (    select 'This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example' example  from dual)
      select 
          substr(t.example, (level-1)*35+1,35)  as subtext
        , length(t.example) l
        , level lv
      from 
        temp t
        connect by level <= trunc(length(t.example)/35+1)
      order by level;
      

      好的,误读了问题..

      【讨论】:

        【解决方案4】:

        正则表达式可能更简单,但您可以使用递归 CTE 或分层查询将字符串拆分为多行,每行 35 个字符:

        with t (str) as (
          select 'This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example.' from dual
        )
        select substr(str, (35 * (level - 1)) + 1, 35) as result
        from t
        connect by level <= ceil(length(str)/35);
        
        RESULT                             
        -----------------------------------
        This is example. This is example. T
        his is example. This is example. Th
        is is example. This is example. Thi
        s is example. This is example. This
         is example.
        
        5 rows selected. 
        

        然后使用listagg()(需要 11gR2 或更高版本)将部件重新粘在一起,中间换行:

        with t (str) as (
          select 'This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example. This is example.' from dual
        )
        select listagg(substr(str, (35 * (level - 1)) + 1, 35), chr(10))
          within group (order by level) as result
        from t
        connect by level <= ceil(length(str)/35);
        
        RESULT                                  
        ----------------------------------------
        This is example. This is example. T
        his is example. This is example. Th
        is is example. This is example. Thi
        s is example. This is example. This
         is example.
        
        1 row selected. 
        

        您不必使用 CTE,您可以在我提到的两个地方重复字符串文字 str,然后从 dual 中选择。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-02
          • 1970-01-01
          • 1970-01-01
          • 2017-02-17
          相关资源
          最近更新 更多