query to remove all characters after last comma in string

前端 未结 3 1341
醉酒成梦
醉酒成梦 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 00:52

    You can use :

    SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)
    

    You can read more here.

    And the update statement :

    UPDATE my_table
        SET my_col = SUBSTRING_INDEX(my_col, ',', 2)
    

    Where you need to replace my_table with your table name and my_col with the column you need to be updated.

    0 讨论(0)
  • 2021-01-08 00:58
    substring_index(col, ',',-1)
    

    will give the string from last index of comma to end of string

    replace(col,concat(',',substring_index(col, ',',-1)),'')
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题