Mysql - 将没有CAP_FIRST的列的多个单词的首字母大写

时间:2016-03-17 21:10:52

标签: mysql regex uppercase

我有一张如下表

    Name
    firstname lastname
    FirstName Lastname
    firstName Lastname
    FirstName lastname

我试图在所有4个案例中将firstnamelastname的每个字母大写为Firstname Lastname

有关查询的任何建议,而不使用CAP_FIRST函数?我可以使用正则表达式吗?

1 个答案:

答案 0 :(得分:5)

这是一个有效的查询:

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;

结果如下:

Name
Firstname Lastname
Firstname Lastname
Firstname Lastname
Firstname Lastname

要更新列使用,

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)));

此致