sql - How to subtract a column of string based on other columns in Hive?
问题描述
With this table, I am trying to remove parts of address that happened to appear in zip_code and city.
+----------------------------------------------+----------+------------+
| address | zip_code | city |
+----------------------------------------------+----------+------------+
| Oceans Group, 12 Pear Tree Road, Derby | DE23 6PY | Derby |
| 970 Stockport Road | M19 3NN | Manchester |
| Cartridge World Guiseley | | Edinburgh |
| 33-41 Kelvin Avenue | G52 4LT | Glasgow |
| Cartridge World Haymarket, 54 Dalry Road, UK | EH5 1HX | Edinburgh |
| 50 Otley Road, Leeds, LS20 8AH, UK | LS20 8AH | |
+----------------------------------------------+----------+------------+
something like
SUBSTR('Oceans Group, 12 Pear Tree Road, Derby', 'DE23 6PY', 'Derby') returns 'Oceans Group, 12 Pear Tree Road, '
SUBSTR('50 Otley Road, Leeds, LS20 8AH, UK', 'LS20 8AH', '') returns '50 Otley Road, Leeds, , UK'
Hope this piece of code save you some time.
CREATE TABLE address_table(
address STRING
, zip_code STRING
, city STRING
);
INSERT INTO address_table VALUES ("Oceans Group, 12 Pear Tree Road, Derby", "DE23 6PY", "Derby");
INSERT INTO address_table VALUES ("970 Stockport Road", "M19 3NN", "Manchester");
INSERT INTO address_table VALUES ("Cartridge World Guiseley", "", "Edinburgh");
INSERT INTO address_table VALUES ("33-41 Kelvin Avenue", "G52 4LT", "Glasgow");
INSERT INTO address_table VALUES ("Cartridge World Haymarket, 54 Dalry Road, UK", "EH5 1HX", "Edinburgh");
INSERT INTO address_table VALUES ("50 Otley Road, Leeds, LS20 8AH, UK", "LS20 8AH", "");
解决方案
Hive 没有常规的字符串替换功能,但您可以使用regexp_replace()
:
select
a.*,
regexp_replace(address, zip_code, '') new_address
from address_table
如果你想要一个update
声明:
update address_table
set address = regexp_replace(address, zip_code, '')
推荐阅读
- mysql - 存在RDB如何通过initContainer初始化mysql?
- python - 如何`requests.post`一个numpy数组
- haproxy - syslog pri 值可以是负数吗?
- typescript - 尝试创建组件时出现 Vue3 问题
- c# - System.Reactive 中的并发订阅者执行
- javascript - 当我的代码没有“失败”时,discord.js“交互失败”
- c# - 在 xml 序列化时连接第三方元素
- react-native - 在 React Native 中集成 Zoho Sign
- python - 使用 for 循环时如何减少 JAX 编译时间?
- excel - 使用 VBA 删除表中除标题以外的所有行的更快方法