Basically I am trying to create a table with a column whose values are extracted from the zipcode portion of an address column in a separate table. The address column is a string that includes street address,city,state, and zipcode each separated by a comma.
FYI The comma format is consistent throughout the dataset.How do I go about setting this up? Below is the code I used to try and create the table.
CREATE TABLE CHILD_TABLE
(ZipCode NUMBER REFERENCES
PARENT_TABLE(substr(address,instr(address,',',-1)+2)));
Here's one option which shows how to extract zipcode out of the address string - using regular expressions, take the last word :
SQL> with addr (addr) as
2 -- this is the "address column"
3 (select 'Muppet street,London,England,12345' from dual)
4 select regexp_substr(addr, '\w+$') zipcode
5 from addr;
ZIPCO
-----
12345
SQL>
[EDIT: insert ZIP codes into another table]
Here's how.
SQL> -- Table that contains addresses
SQL> create table t1 (addr varchar2(50));
Table created.
SQL> insert into t1
2 select 'Muppet street,London,England,12345' from dual union all
3 select 'Batman road,Berlin,Germany,9948' from dual union all
4 select 'Ilica,Zagreb,Croatia,10000' from dual;
3 rows created.
SQL> -- A new table, containing ZIP codes
SQL> create table t2 (zipcode number);
Table created.
SQL> -- Insert zip codes from the address into the new table
SQL> insert into t2 (zipcode)
2 select regexp_substr(t1.addr, '\w+$')
3 from t1;
3 rows created.
SQL> select * From t2;
ZIPCODE
----------
12345
9948
10000
SQL>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.