Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Oracle 9i中从一列中提取邮政编码/数字并将其放入另一列_Sql_Regex_Substring_Trim_Oracle9i - Fatal编程技术网

如何在SQL Oracle 9i中从一列中提取邮政编码/数字并将其放入另一列

如何在SQL Oracle 9i中从一列中提取邮政编码/数字并将其放入另一列,sql,regex,substring,trim,oracle9i,Sql,Regex,Substring,Trim,Oracle9i,表地址包含列[ID、街道、城镇、邮政编码、国家] Oracle 9i数据库中的Hot to make语句: 如果列COUNTRY包含字符串'UK',并且POSTCODE为空,但列TOWN以邮政编码(格式为xxxxx或xx xxx的数字)开头,则将邮政编码移动到列POSTCODE并从列TOWN中删除邮政编码,下面是两条sql语句,一条可供选择,并查看update语句和update语句的影响。在运行任何更新之前备份您的表,并始终通过首先以select语句运行更新来测试更新是否达到了预期效果 注意:我

地址
包含列
[ID、街道、城镇、邮政编码、国家]

Oracle 9i数据库中的Hot to make语句:


如果列
COUNTRY
包含字符串
'UK'
,并且
POSTCODE
为空,但列
TOWN
以邮政编码(格式为xxxxx或xx xxx的数字)开头,则将邮政编码移动到列
POSTCODE
并从列
TOWN
中删除邮政编码,下面是两条sql语句,一条可供选择,并查看update语句和update语句的影响。在运行任何更新之前备份您的表,并始终通过首先以select语句运行更新来测试更新是否达到了预期效果

注意:我假设您希望保留最初输入的邮政编码格式。例如,如果输入为xxxxx,则将保存为xxxxx,如果输入为xx xxx,则将保存为xx xxx

检查以查看update语句将执行的操作:

select ID, STREET, TOWN, POSTCODE, COUNTRY, 
case when substr(TOWN, 3, 1) = '-' like '%-%' then trim(substr(TOWN,7, length(TOWN)-6)) else trim(substr(TOWN,6, length(TOWN)-5)) end as NEW_TOWN,
case when substr(TOWN, 3, 1) = '-' like '%-%' then substr(TOWN, 1, 6) else substr(TOWN, 1, 5) end as NEW_POSTCODE--Assumes you want to keep the dash if it exists
from ADDRESSES
where COUNTRY like'%UK%' --contains string UK
and trim(POSTCODE) is null -- postcode is empty
and (
    length(trim(translate(substr(TOWN, 1, 5), '0123456789', ' '))) is null -- town starts with xxxxx digits
    or
    (length(trim(translate(substr(TOWN, 1, 2)||substr(TOWN,4,3, '0123456789', ' '))))  and  substr(TOWN, 3, 1) = '-') -- town starts with xx-xxx digits
    )
;
如果您满意,请运行update语句

update ADDRESSES
set
TOWN = case when substr(TOWN, 3, 1) = '-' like '%-%' then trim(substr(TOWN,7, length(TOWN)-6)) else trim(substr(TOWN,6, length(TOWN)-5)) end,
POSTCODE = case when substr(TOWN, 3, 1) = '-' like '%-%' then substr(TOWN, 1, 6) else substr(TOWN, 1, 5) end --Assumes you want to keep the dash if it exists
from ADDRESSES
where COUNTRY like'%UK%' --contains string UK
and trim(POSTCODE) is null -- postcode is empty
and (
    length(trim(translate(substr(TOWN, 1, 5), '0123456789', ' '))) is null -- town starts with xxxxx digits
    or
    (length(trim(translate(substr(TOWN, 1, 2)||substr(TOWN,4,3, '0123456789', ' '))))  and  substr(TOWN, 3, 1) = '-') -- town starts with xx-xxx digits
    )
;

我希望这至少可以作为一个起点

我也没有O9i来测试这个,所以我怀疑我的答案是没有用的,但我想我可以尝试一下,只是为了好玩。我希望以下内容能给你或某人一个起点。以下seam将在postgresql 9.3中工作:-)

查询以更新类似12345的邮政编码

update addresses set 
postcode=substring(town, '^([0-9]{5})'),
town=substring(town, '^[0-9]{5}\s(.*)')
where country like '%UK%' and 
(postcode is null or postcode='') and 
town ~ E'^[0-9]{5} ';
查询以更新类似12-123的邮政编码

update addresses set 
postcode=substring(town, '^([0-9]{2}\-[0-9]{3})'),
town=substring(town, '^[0-9]{2}\-[0-9]{3}\s([\w\d\-\s]+)')
where country like '%UK%' and 
(postcode is null or postcode='') and 
town ~ E'^[0-9]{2}\-[0-9]{3} ';
结果

dawid=# select * from addresses ;
 id | street |       town        | postcode | country 
----+--------+-------------------+----------+---------
  5 | 5      | 55555 dupa jasio5 |          | PL
  2 | 2      | dupa jasio2       | 22222    | UK
  4 | 4      | dupa jasio4       | 44444    | UK
  1 | 1      | dupa jasio1       | 11-111   | UK
  3 | 3      | dupa jasio3       | 33-333   | UK

您可以添加一些samlpe数据,并希望得到输出吗?您应该向我们展示您尝试过的内容以及失败的原因,并提供示例数据和预期结果。我们是来帮助你的,不是为你做所有的工作的。@dawciobiel-如果你从Gordon的regex/left代码中得到了ORA-00904而不是ORA-00920(无效的关系运算符),那么你不是真的在使用Oracle 9i,那么你能在添加示例数据等时用你正在使用的实际版本更新并重新标记问题吗?不,我不能,因为我现在使用的是另一个数据库,而不是问题所在。换句话说,解决方案应该在v9i上工作——并且没有常规的expressions@dawciobiel-因为
like
只识别
%
通配符,所以它不识别任何正则表达式语法;这就是为什么在10g中添加了一个单独的类似于的regexp_。好吧,不管怎样,thx。我再也不能测试那个答案了。但无论如何我都会接受的。嗯,不管怎样,谢谢。我也无法再测试这个答案了。你仍然在使用正则表达式,这在9i中是不受支持的。
dawid=# select * from addresses ;
 id | street |       town        | postcode | country 
----+--------+-------------------+----------+---------
  5 | 5      | 55555 dupa jasio5 |          | PL
  2 | 2      | dupa jasio2       | 22222    | UK
  4 | 4      | dupa jasio4       | 44444    | UK
  1 | 1      | dupa jasio1       | 11-111   | UK
  3 | 3      | dupa jasio3       | 33-333   | UK