2013-06-19 227 views
1

这里我的笔记列中有2000个字符最大的问题,我想我的字符串输出基于35个字符,雅我需要替换字符串中的30个字符后<br>标记..例如字符串“嗨,你好,你在那里做什么,需要你的帮助!”,我需要出来作为“你好你好,你如何做出<br>那里,需要你的帮助!类似的,我需要计算刺的长度,必须把它分解35 + 35 + 35 ..我不知道如何在SQL/PLSQL执行此字符串拆分/字符串替换基于字符长度

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

回答

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; 

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

嗨,你好,你好吗?
e,需要你的帮助!

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

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! 
*/