示例文字: 温哥华观点,不列颠哥伦比亚省温哥华市West 73rd Avenue 606号1200号,V6P 6G5
我需要从文本中提取不列颠哥伦比亚省。
谢谢
答案 0 :(得分:2)
这应该绝对正常。
select substring_index(substring_index('Viewpoint Vancouver, 1200 West 73rd Avenue Suite 606, Vancouver, British Columbia, V6P 6G5',',', -2),',',1)
在此处查看有关子字符串索引的文档-https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index
答案 1 :(得分:0)
我推荐MontyPython解决方案,但想添加一个正则表达式解决方案。 MySQL支持从version 8开始的正则表达式,MariaDB从version 10.0.5开始就支持正则表达式。这是MariaDB的示例:
SELECT REGEXP_REPLACE('...', '.*,(.*),', '\\1');
示例:
MariaDB [(none)]> select REGEXP_REPLACE('Viewpoint Vancouver, 1200 West 73rd Avenue Suite 606, Vancouver, British Columbia, V6P 6G5', '.*,(.*),.*', '\\1');
+-----------------------------------------------------------------------------------------------------------------------------------+
| REGEXP_REPLACE('Viewpoint Vancouver, 1200 West 73rd Avenue Suite 606, Vancouver, British Columbia, V6P 6G5', '.*,(.*),.*', '\\1') |
+-----------------------------------------------------------------------------------------------------------------------------------+
| British Columbia |
+-----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)