2013-06-19 6 views
1

최대 2000 자의 메모 열에 문제가 있습니다. 문자 수를 기준으로 문자열 출력을 원합니다. 예를 들어 <br> 태그를 문자열의 30 자 뒤에 바꿔야합니다. 예 문자열 "안녕하세요 안녕하세요 당신은 거기서하고, 당신의 도움이 필요하십니까!", 나는 밖으로 넣어 필요가 안녕하세요 안녕하세요 어떻게 <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; 

그러나 이것은 중간 단어를 깨뜨릴 가능성이 큽니다.

안녕하세요 안녕하세요 안녕하세요. 어떻게 당신은 당신의 도움이 필요하십니까
안녕하세요.

공백 만 사용하려면 더 복잡한 코드를 작성해야합니다.

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