Sql – How to order by last name on a full name column

sqlsql server

I have a SQL Table with a column called FullName which contains, for example, "John Smith".

How can order the data by the last name that appears in the FullName column?

For a long name like "Laurence John Fishburne", I would like to order the data by the word "Fishburne".

Thus, names are stored in the order

  • First Name
  • Middle Names
  • Last Name

I am using Microsoft SQL Server 2005.

Best Answer

I would do something like:

SELECT FullName
FROM TABLE
ORDER BY REVERSE(SUBSTRING(REVERSE(FullName), 0, CHARINDEX(' ', REVERSE(FullName))))