query to remove all characters after last comma in string

前端 未结 3 1376
醉酒成梦
醉酒成梦 2021-01-08 00:36

i have a mysql table with this sort of data

TACOMA, Washington, 98477

Now i have thousands of such rows. I want the data to be manipulated

3条回答
  •  一个人的身影
    2021-01-08 01:02

    Possibly this way. Count the number of commas (by checking the length against the length with all the commas removed) and then use SUBSTRING_INDEX to get the string up to the number of commas:-

    SELECT SUBSTRING_INDEX(col, ',', LENGTH(col) - LENGTH(REPLACE(col, ',', '')))
    FROM SomeTable
    

提交回复
热议问题