[英]Oracle SQL Extracting A Value From Another Column into a New Column?
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. 基本上我正在尝试创建一个表,其中的列的值是从单独表中的地址列的zipcode部分中提取的。 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 : 这里有一个选项,显示如何从地址字符串中提取zipcode - 使用正则表达式,取最后一个字 :
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.