开发者

Is it possible to use characters instead of a position in a substring function?

is it possible to use characters instead of a position in a substring function?

SELECT SUBSTRING(title,2) FR开发者_如何学JAVAOM table
puts out every title starting with the second position.

Now I want to cut the output after a space. The space positions varies. Is it realiseable?

I tried sth. like

SUBSTRING(title,2,LOCATE('',title))
,but for some reason the output was empty.

Thank you in advance. Soloco


You can use SUBSTRING_INDEX for that:

SELECT SUBSTRING_INDEX(title, ' ', 1) FROM table
SELECT SUBSTRING_INDEX('hello world', ' ', 1) # gives you 'hello'

http://dev.mysql.com/doc/refman/5.0/en/string-functions.htm


In your LOCATE statement, you are searching for an empty string '' instead of a space ' '

Add a space character.

What you are doing will fetch the string from its second character to the first occurrence of a space character. Is this really what you want to do? What if the the first or second character are space characters?


You could use:

SUBSTRING(title,2,charindex('',title)-1)

This would cut the string just before the space in the title

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜