对于您发布的示例数据,一个选项可能是:
SQL> with test (col) as
2 (select '{"firstName":"Curtis","street2":null,"city":"Milton"}' from dual union all
3 select '"lastName":"C Fugatt","street1":"4146 Audiss Rd.","city":"Los Angeles"}' from dual
4 )
5 select regexp_replace(substr(col, instr(col, 'city') + 7), '[^[:alnum:] ]', '') result
6 from test;
RESULT
--------------------------------------------------------------------------------
Milton
Los Angeles
SQL>
它有什么作用?
-
substr 找到 city 字符串的第一个位置并在其上添加 7(以跳过 city 本身、双引号和冒号)并且 - 结果 - 将所有内容返回到字符串的末尾
-
regexp_replace 删除字母数字和空格以外的任何内容
- 剩下的是城市名称(即最终结果)
当您改变主意时(因此city 不是字符串中的最后一个信息),一个选项可能是嵌套的SUBSTR:
SQL> with test (col) as
2 (select '{"firstName":"Curtis","street2":null,"city":"Milton"}' from dual union all
3 select '"lastName":"C Fugatt","street1":"4146 Audiss Rd.","city":"Los Angeles"}' from dual union all
4 select '"firstName":"Curtis","lastName":"C Fugatt","street1":"4146 Audiss Rd.","street2":null,"city":"Milton","state":"FL","zip":"32583","country":"USA","phone":"' from dual
5 )
6 select
7 substr(substr(col, instr(col, 'city') + 7), -- everything that follows "city"
8 1, -- starting from the 1st position
9 instr(substr(col, instr(col, 'city') + 7), '"') - 1 -- up to the first double quote in that "everything that follows "city"" substring
10 ) result
11 from test;
RESULT
--------------------------------------------------------------------------------
Milton
Los Angeles
Milton
SQL>