简体   繁体   中英

how can I extract text from second last comma till last comma in mysql?

sample text: Viewpoint Vancouver, 1200 West 73rd Avenue Suite 606, Vancouver, British Columbia, V6P 6G5

I need to extract British Columbia from the text.

Thanks

This should work absolutely fine.

select substring_index(substring_index('Viewpoint Vancouver, 1200 West 73rd Avenue Suite 606, Vancouver, British Columbia, V6P 6G5',',', -2),',',1)

Check out the documentation for Substring Index here - https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index

I recomment the solution by MontyPython, but would like to add a regular expression solution. MySQL supports regular expressions starting from version 8 , MariaDB has it since version 10.0.5 . Here's an example for MariaDB:

SELECT REGEXP_REPLACE('...', '.*,(.*),', '\\1');

Example:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM