字符串拆分/字符串替换基于字符长度

时间:2013-06-19 08:23:56

标签: sql string plsql split

这里我的问题在一个最多2000个字符的备注栏中,我希望我的字符串输出基于35个字符,你需要在字符串中的30个字符后替换<br>标签。示例字符串“嗨你好怎么样你在外面做什么,需要你的帮助!“,我需要把它作为”你好你好,你在做什么&lt; br&gt;那里,需要你的帮助!类似我需要计算刺痛长度并且必须将它分开35 + 35 +35 ..我不知道如何在sql / plsql中执行此操作。

select substr(note,1,(instr(note, ' ',35)))||'<br>'||substr(note,instr(note, ' ',35),
     (instr(note, ' ',35)))notes from test

2 个答案:

答案 0 :(得分:2)

DECLARE
    CURSOR notes_cur IS
        SELECT  1 note_id, 'hi hello how are you doing out there, need your help! hi hello how are you doing out there, need your help!' note FROM DUAL UNION ALL
        SELECT  2,         'hi hello how are you doing out there, need your help! hi hello how are you doing out there, need your help!' note FROM DUAL;

    TYPE notes_ntt IS TABLE OF notes_cur%ROWTYPE;
    l_notes          notes_ntt;
    l_loop_counter   NUMBER;
    l_split          notes_ntt := notes_ntt();
    l_space_start    NUMBER;
    l_string_start   NUMBER;
    l_space_position NUMBER;
BEGIN
    OPEN  notes_cur;
    FETCH notes_cur BULK COLLECT INTO l_notes;
    CLOSE notes_cur;

    FOR indx IN 1..l_notes.COUNT LOOP
        l_space_start    := 33;
        l_string_start   := 1;
        l_loop_counter   := TRUNC(LENGTH(l_notes(indx).note) / 35);

        FOR note IN 1..l_loop_counter LOOP
            l_split.EXTEND;
            l_split(l_split.LAST).note_id := l_notes(indx).note_id;

            l_space_position   := INSTR(l_notes(indx).note, CHR(32), l_space_start, 1);

            l_split(l_split.LAST).note :=   SUBSTR
                                            (
                                                l_notes(indx).note
                                            ,   l_string_start
                                            ,   CASE
                                                    WHEN l_space_position = 0
                                                    THEN l_string_start
                                                    ELSE l_space_position - l_string_start
                                                END
                                            ) || CHR(10);

            l_space_start  := l_space_position + 33;
            l_string_start := l_space_position + 1;
        END LOOP;
    END LOOP;

    FOR indx IN 1..l_split.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE(l_split(indx).note_id || ' ' || l_split(indx).note);
        NULL;
    END LOOP;
END;
/*
1 hi hello how are you doing out there,

1 need your help! hi hello how are

1 you doing out there, need your help!

2 hi hello how are you doing out there,

2 need your help! hi hello how are

2 you doing out there, need your help!
*/

答案 1 :(得分:1)

你可以这样做:

declare
   l_in_string varchar2(1000) := 'hi hello how are you doing out there, need your help!';
   l_out_string varchar2(1000);
begin
   while length(l_in_string) > 35 loop
      l_out_string := l_out_string || substr(l_in_string, 1, 35) || '<br>';
      l_in_string := substr(l_in_string, 36);
   end loop;
   l_out_string := l_out_string || l_in_string;
   dbms_output.put_line(l_out_string);
end;

然而,这很可能打破中间词,例如

  

你好,你好吗,需要你的帮助!

如果你想只打破空格,你需要编写更复杂的代码。