Вопрос

I have a table with one column with the below data

head1  [00100 - 00228] lab66  [lab661]

I have one more table with two column I need to insert the first part in one column and the second part of data in one column without []

Table one

Column1
--------------------------------------
head1  [00100 - 00228] lab66  [lab661]

Note: the two data is in one row

Table two

column1 | column2
head1       00100 - 00228
lab66       lab661

2 column and 2 rows

Can anyone please help me to accomplish this task

Это было полезно?

Решение

you can use regular expressions to accomplish the task. try

 INSERT
   INTO table2 trg ( col1, col2 )
        SELECT RTRIM(REGEXP_REPLACE(src.col1, '^([^'||chr(91)||']+)\[[^'||chr(93)||']+\].*$', '\1'))
             , LTRIM(REGEXP_REPLACE(src.col1, '^[^'||chr(91)||']+\[([^'||chr(93)||']+)\].*$', '\1'))
          FROM table1 src
      ;

explanation:

  • the regex pattern describes the structure of your source column content (text + text in brackets). note that ...

    • each record's column must match this structure.
    • the complete content of the column is matched due to the use of ^, $ in the pattern.
    • [, ] carry semantics in regular expressions, thus they have to be escaped to mach the literal chars. edit the canonical escaping (\[, \]) does not work, so you have to express the symbols using oracles chr funciton (remember basic ? :-)))
  • the parentheses within the regex denote 'interesting' parts of the data that matches the pattern - for the first columnn, that is the text before the brackets, for the second its the part inside of them.

  • the REGEXP_REPLACE function replaces the part of the source column matching the pattern with the replacement string. as the pattern matches the whole string, it replaces the whole string.

  • the replacement string contains a so-called 'backreference' to the first 'interesting portion' of the match - which is the part corresponding to the pattern part enclosed in parentheses.

  • the trim functions remove trailing/leading whitespace.

edit: test script available here

edit #2: The op actually has 2 data sections to be partitioned in 1 column of the source table. This can be handled by 2 insert statements (or a union in the select of 1 statement) by upgrading the pattern for matching the 2nd item:

INSERT
       INTO test_tab2 trg ( col1, col2 )
            SELECT RTRIM(REGEXP_REPLACE(REPLACE(REPLACE(src.col1, CHR(10), ' '), CHR(13), ' '), '^([^'||chr(91)||']+)\[[^'||chr(93)||']+\].*$', '\1'))
                 , LTRIM(REGEXP_REPLACE(REPLACE(REPLACE(src.col1, CHR(10), ' '), CHR(13), ' '), '^[^'||chr(91)||']+\[([^'||chr(93)||']+)\].*$', '\1'))
             FROM test_tab1 src
         ;
INSERT
       INTO test_tab2 trg ( col1, col2 )
            SELECT RTRIM(REGEXP_REPLACE(REPLACE(REPLACE(src.col1, CHR(10), ' '), CHR(13), ' '), '^[^'||chr(91)||']+\[[^'||chr(93)||']+\][ ]*([^'||chr(91)||']+)\[[^'||chr(93)||']+\].*$', '\1'))
                 , LTRIM(REGEXP_REPLACE(REPLACE(REPLACE(src.col1, CHR(10), ' '), CHR(13), ' '), '^[^'||chr(91)||']+\[[^'||chr(93)||']+\][ ]*[^'||chr(91)||']+\[([^'||chr(93)||']+)\].*$', '\1'))
              FROM test_tab1 src
          ;

test script available here

edit #3: the whitespace separating the 2 data sections may contain line feeds/carriage returns. for simplified processing, added ordinary replace calls in select clauses.

test script available here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top