Running the SUBSTRING_INDEX query on a column

时间:2016-04-07 10:42:34

标签: mysql join

I have this column called location in a table called raw_data. Each row have lots of varchar data separated by [, ]. Examples

Row 1 dusseldorf, kjsbdfygs4, Germany

Row 2 768768745h, kiev, Ukraine

Row 3 %%%%666, Accra, Ghana

Yes some make no sense. Im trying to select the last part of the string which is the country and display it. I tried using the substring index query and understand how it works but cant seem to understand how to apply it to a column.

2 个答案:

答案 0 :(得分:1)

You can use as per below-

SELECT SUBSTRING_INDEX(my_column,',',-1) FROM raw_data;

Note: If you need 1st part then use only 1 instead of -1.

答案 1 :(得分:0)

use like this

SELECT SUBSTRING_INDEX(location ,',',2);  

go to below link for explanation

http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php