1

Here my problem in a notes column having 2000 characters max, i want my string output based on 35 characters, ya i need to replace <br> tag after 30 characters in the string.. example string "hi hello how are you doing out there, need your help!", i need out put as "hi hello how are you doing out<br> there, need your help! similar i need to calculate the sting length and have to split it 35+35+35.. i don't know how to perform this in sql/plsql.

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

2 回答 2

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!
*/
于 2013-06-19T11:15:08.627 回答
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;

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

嗨,你好,你在外面怎么样
,需要你的帮助!

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

于 2013-06-19T10:25:37.030 回答