问题
I have a table such as the below
Name
firstname lastname
FirstName Lastname
firstName Lastname
FirstName lastname
I am trying to capitalize each letter of the firstname
and lastname
to be Firstname Lastname
in all the 4 cases.
Any recommendations on the query without using the CAP_FIRST function? Can I use regex?
回答1:
This is a query that works:
SELECT
CONCAT(
UPPER(SUBSTRING(Name,1,1)),
LOWER(SUBSTRING(Name,2,Locate(' ', Name)-1)),
UPPER(SUBSTRING(Name,Locate(' ', Name)+1,1)),
LOWER(SUBSTRING(Name,Locate(' ', Name)+2)))
FROM NameTable;
This the result:
Name
Firstname Lastname
Firstname Lastname
Firstname Lastname
Firstname Lastname
To UPDATE column use,
UPDATE NameTable
SET NameTable.Name = CONCAT(
UPPER(SUBSTRING(Name,1,1)),
LOWER(SUBSTRING(Name,2,Locate(' ', Name)-1)),
UPPER(SUBSTRING(Name,Locate(' ', Name)+1,1)),
LOWER(SUBSTRING(Name,Locate(' ', Name)+2)));
Regards
来源:https://stackoverflow.com/questions/36071748/mysql-capitalize-first-letter-of-multiple-words-of-a-column-without-cap-first