2013-06-19 225 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! 
*/