Jump to content


This topic is now archived and is closed to further replies.


ORDER BY the SECOND word in a field

Recommended Posts

I have a situation where I am needing to sort a list alphabetically by the last name. However, due to BAD planning, all of the names are stored in a single field like this
[i]firstname lastname[/i]
What is going to be my best way to sort my list? I looked through string manipulation in MYSQL, but I couldn't find a way to doa select statment that would search through my table, and pull all names based on the lastname.
SELECT * from catalog ORDER BY . . .
and here I want to say
"the first character after the first space in " FULLNAME

Any ideas?

Share this post

Link to post
Share on other sites
I can't give you a straight answer, but I see that you can use regexp in MySQL... here's just one page I found, but it should be a good starting point....

[a href=\"http://www.tech-recipes.com/mysql_tips484.html\" target=\"_blank\"]one page discussing regular expressions in MySQL selects[/a]

I'd highly recommend exporting the data, and re-importing it into two tables though, because it'll save you time down the road when any similar situations arise.

Share this post

Link to post
Share on other sites
This will sort based on the second word (won't catch middle names correctly).

[code]SELECT * from catalog ORDER BY SUBSTR(LTRIM(fullname), LOCATE(' ',LTRIM(fullname)))[/code]

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.