Mysql - Capitalize first letter of multiple words of a column without CAP_FIRST

妖精的绣舞 提交于 2021-01-29 04:56:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!