I would like to capitalize names properly, which in this case means:
- The first letter is capitalized.
- The first letter after a space is capitalized (‘Van Helsing’, not ‘Van helsing’)
- The first letter after a dash is capitalized (‘Johnson-Smith’, not ‘Johnson-smith’)
- No other letters are capitalized.
The first and last requirements are easily handled:
CONCAT(LEFT(name, 1), LOWER(RIGHT(name, LENGTH(name) - 1)))
The others are harder. I’ve written a 54-times-nested REPLACE statement (not by hand of course, I used Excel)
REPLACE(REPLACE(REPLACE(REPLACE(...,' b',' B'),'-b','-B'),' a',' A'),'-a','-A')
but I feel like there must be a more elegant and maintainable solution. Any ideas?
If there’s a built-in function that is similar but not identical to my requirements that would probably be fine.
Edit: This script will only run on names that have lost their capitalization already, so there’s no danger in mis-handling obscure names. Handling apostrophes like spaces and dashes would be nice, though. A glance through the current data shows that many (~30%) of the names have at least one of [-‘ ].
The code frustratedwithforms posted does not work correctly…it correctly capitalizes the first and last words in the string and deletes anything in between (if there are > 2). Someone posted a corrected version at the URL he posted…
(See Matt Cavanaugh’s post on May 15 2009 3:52pm at: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html)