How to remove part of string in mysql?

后端 未结 5 1781
既然无缘
既然无缘 2020-12-15 20:27

In one table of my database I have strings which looks like this one:

sometext-othertext

How to remove the text including dash with SELECT

5条回答
  •  误落风尘
    2020-12-15 21:24

    Return the substring before the first occurrence of the delimiter "-":

    SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 1) as result;

    Outputs result = "foo"

    You can replace 1 with the numbers of occurrences you want before getting the substring

    SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 2) as result;

    Outputs result = "foo-bar"

    Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

提交回复
热议问题