子串索引mysql

时间:2015-01-13 08:17:31

标签: mysql substring

我需要帮助mysql中的Substring索引函数。我需要将以下字符串的一部分从开始模式返回到字符串的结尾:

 Michael Jackson Jannet  ->  SubString 1: Michael   / SubString 2: Jackson Jannet
 Lincoln Obama Kennedy Washington  ->  SubString 1: Lincoln / SubString 2: Obama Kennedy Washington

2 个答案:

答案 0 :(得分:0)

InStr()函数是否符合您的需求? 搜索字符串中的第一个空格,您现在知道该位置。然后使用Left()和Mid():

在两个子串中剪切字符串
pos = InStr("Michael Jackson Janet"," ")
substring1 = Left("Michael Jackson Janet", pos - 1)
substring2 = Mid("Michael Jackson Janet", pos)

如果你坚持使用SubString()函数,你可以这样做:

pos = InStr("Michael Jackson Janet"," ")
substring1 = SubString("Michael Jackson Janet", 1, pos - 1)
substring2 = SubString("Michael Jackson Janet", pos + 1)

这对你有帮助吗?

答案 1 :(得分:0)

select substring('Michael Jackson Jannet',1,(locate (' ','Michael Jackson Jannet'))) as firstNBame,
      substring('Michael Jackson Jannet',(locate (' ','Michael Jackson Jannet'))) as secondName;