开发者

How to select the last word in a string

开发者 https://www.devze.com 2023-01-21 11:47 出处:网络
I have used SELECT SUBST(field_name, 1, Loc开发者_Go百科ate(\'\',field_name)) AS first_word FROM table_name

I have used

SELECT
    SUBST(field_name, 1, Loc开发者_Go百科ate('',field_name)) AS first_word
      FROM
    table_name 

and it works for the first word so I tried using last_word but still get the first word so I guess there must be a differet way??


Select SUBSTRING_INDEX(field_name,' ',-1)

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


Well, you probably could do:

SELECT REVERSE( SUBST( REVERSE( field_name ), 1, LOCATE( ' ', REVERSE( field_name )))

This likely won't work if your field has trailing spaces or is a single word.

0

精彩评论

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