-
-
Save thevacs/adbf5a5be9517c7832bc838d275a96c2 to your computer and use it in GitHub Desktop.
MySQL: Extract last and first name(s) from a full "name" field (for Elgg)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
-- Assumed to be trimmed | |
name | |
-- Does name contain multiple words? | |
,(LOCATE(' ', name) = 0) AS hasMultipleWords | |
-- Returns the end of the string back until reaches a space. | |
-- E.g. "John Doe" => "Doe" | |
-- E.g. "Francis Scott Key" => "Key" | |
-- E.g. "Prince" => "Prince" | |
,REVERSE(SUBSTRING_INDEX(REVERSE(name), ' ', 1)) AS lastName | |
-- Everything not included in the last name. | |
-- E.g. "John Doe" => "John" | |
-- E.g. "Francis Scott Key" => "Francis Scott" | |
-- E.g. "Prince" => "" | |
,TRIM(SUBSTRING(name, 1, CHAR_LENGTH(name) - CHAR_LENGTH(SUBSTRING_INDEX(REVERSE(name), ' ', 1)))) AS firstNames | |
FROM `elgg_users_entity` | |
LIMIT 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment