Oracle: Split text field on newline

痞子三分冷 提交于 2019-12-11 11:55:54

问题


I need to merge a couple of tables, once of which has a single Address field, the 2nd which as 3 address line fields.

The single address field in the 1st table includes line breaks.

How can I split this field into 3?

Update:

Turns out some records had CHR(10) || CHR(13), others CHR(13) || CHR(10), and yet others just CHAR(13). Regardless, the accepted solution below works. A useful way to tell is

select dump(field) from table;

回答1:


Assuming that your linebreak character is CHR(10), something like the following should work:

SELECT TRIM(REGEXP_REPLACE(addr, '(.*)' || CHR(10) || '.*' || CHR(10) || '.*', '\1')) AS STREET_ADDR,
       TRIM(REGEXP_REPLACE(addr, '.*' || CHR(10) || '(.*)' || CHR(10) || '.*', '\1')) AS CITY,
       TRIM(REGEXP_REPLACE(addr, '.*' || CHR(10) || '.*' || CHR(10) || '(.*)', '\1')) AS STATE
      FROM addr_table; 

If addr_table is populated using the following statement:

INSERT INTO addr_table(addr)
VALUES('12345 MY STREET' || CHR(10) || 'NOWHERESVILLE' || CHR(10) || 'ASTATE');

the above SELECT will return

STREET_ADDR     CITY            STATE
12345 MY STREET NOWHERESVILLE   ASTATE

Share and enjoy



来源:https://stackoverflow.com/questions/5721106/oracle-split-text-field-on-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!