问题
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