我有一个现有的表'people_table',其中包含一个字段full_name
。
许多记录的'full_name'字段填充了错误的大小写。例如'fred Jones'
或'fred jones'
或'Fred jones'
。
我可以通过以下方式找到这些错误条目:
SELECT * FROM people_table WHERE full_name REGEXP BINARY '^[a-z]';
如何将找到的每个单词的首字母大写?例如'fred jones'
变为'Fred Jones'
。
答案 0 :(得分:54)
没有MySQL功能,你必须自己编写。在以下链接中有一个实现:
http://joezack.com/index.php/2008/10/20/mysql-capitalize-function/
要使用它,首先需要在数据库中创建该功能。您可以这样做,例如,使用MySQL Query Browser(右键单击数据库名称并选择Create new Function)。
创建函数后,您可以使用如下查询更新表中的值:
UPDATE users SET name = CAP_FIRST(name);
答案 1 :(得分:29)
如果你只需要运行一次,并且你不想创建一个函数,那么你可以做一些真正被扼杀的事情:
UPDATE people_table SET full_name = LOWER(full_name);
UPDATE people_table SET full_name = CONCAT(UPPER(SUBSTR(full_name,1,1)),LOWER(SUBSTR(full_name,2)));
UPDATE people_table SET full_name = REPLACE(full_name,' a',' A');
UPDATE people_table SET full_name = REPLACE(full_name,' b',' B');
UPDATE people_table SET full_name = REPLACE(full_name,' c',' C');
UPDATE people_table SET full_name = REPLACE(full_name,' d',' D');
UPDATE people_table SET full_name = REPLACE(full_name,' e',' E');
UPDATE people_table SET full_name = REPLACE(full_name,' f',' F');
UPDATE people_table SET full_name = REPLACE(full_name,' g',' G');
UPDATE people_table SET full_name = REPLACE(full_name,' h',' H');
UPDATE people_table SET full_name = REPLACE(full_name,' i',' I');
UPDATE people_table SET full_name = REPLACE(full_name,' j',' J');
UPDATE people_table SET full_name = REPLACE(full_name,' k',' K');
UPDATE people_table SET full_name = REPLACE(full_name,' l',' L');
UPDATE people_table SET full_name = REPLACE(full_name,' m',' M');
UPDATE people_table SET full_name = REPLACE(full_name,' n',' N');
UPDATE people_table SET full_name = REPLACE(full_name,' o',' O');
UPDATE people_table SET full_name = REPLACE(full_name,' p',' P');
UPDATE people_table SET full_name = REPLACE(full_name,' q',' Q');
UPDATE people_table SET full_name = REPLACE(full_name,' r',' R');
UPDATE people_table SET full_name = REPLACE(full_name,' s',' S');
UPDATE people_table SET full_name = REPLACE(full_name,' t',' T');
UPDATE people_table SET full_name = REPLACE(full_name,' u',' U');
UPDATE people_table SET full_name = REPLACE(full_name,' v',' V');
UPDATE people_table SET full_name = REPLACE(full_name,' w',' W');
UPDATE people_table SET full_name = REPLACE(full_name,' x',' X');
UPDATE people_table SET full_name = REPLACE(full_name,' y',' Y');
UPDATE people_table SET full_name = REPLACE(full_name,' z',' Z');
答案 2 :(得分:15)
如果您想要大写所有单词,则需要调用自定义函数。
-- may help:
-- DROP function if exists capitalize;
DELIMITER $$
CREATE FUNCTION `capitalize`(s varchar(255)) RETURNS varchar(255)
BEGIN
declare c int;
declare x varchar(255);
declare y varchar(255);
declare z varchar(255);
set x = UPPER( SUBSTRING( s, 1, 1));
set y = SUBSTR( s, 2);
set c = instr( y, ' ');
while c > 0
do
set z = SUBSTR( y, 1, c);
set x = CONCAT( x, z);
set z = UPPER( SUBSTR( y, c+1, 1));
set x = CONCAT( x, z);
set y = SUBSTR( y, c+2);
set c = INSTR( y, ' ');
end while;
set x = CONCAT(x, y);
return x;
END$$
DELIMITER ;
现在你这样做:
UPDATE mytable SET thefield = capitalize(thefield);
答案 3 :(得分:4)
我尝试了上面的代码但是在函数上有语法错误,因此无法创建它。如果它可以帮助任何人
,请为最新版本的MySQL写这个CREATE FUNCTION `CAP_FIRST`(input VARCHAR(255)) RETURNS varchar(255) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE len INT;
DECLARE i INT;
DECLARE charnum INT;
declare SortedName varchar(255);
SET len = CHAR_LENGTH(input);
SET input = LOWER(input);
SET i = 1;
set charnum = 1;
set SortedName = '';
WHILE (i <= len) DO
if charnum = 1 then
set SortedName = concat(SortedName,upper(mid(input,i,1)));
set charnum = charnum + 1;
else
if mid(input,i,1) = ' ' then
set SortedName = concat(SortedName,' ');
set charnum = 1;
else
set SortedName = concat(SortedName,mid(input,i,1));
set charnum = charnum + 1;
end if;
end if;
SET i = i + 1;
END WHILE;
RETURN SortedName;
END
答案 4 :(得分:4)
DELIMITER $$
CREATE FUNCTION `capitalize`(s varchar(255)) RETURNS varchar(255)
BEGIN
declare c int;
declare x varchar(255);
declare y varchar(255);
declare z varchar(255);
set x = UPPER( SUBSTRING( s, 1, 1));
set y = lower(SUBSTR( s, 2));
set c = instr( y, ' ');
while c > 0
do
set z = SUBSTR( y, 1, c);
set x = CONCAT( x, z);
set z = UPPER( SUBSTR( y, c+1, 1));
set x = CONCAT( x, z);
set y = SUBSTR( y, c+2);
set c = INSTR( y, ' ');
end while;
set x = CONCAT(x, y);
return x;
END$$
DELIMITER ;
Create above function to set First character to capital of each words
答案 5 :(得分:3)
以下是尼古拉斯·汤普森的两个有用的功能。您可以将UC_DELEMITER的第三个变量设置为false,将第二个变量设置为&#34; &#34;对于多个单词的大写。
<强> UC_FIRST 强> 大写任何给定的字符串 - 此函数是PHP中ucfirst函数的克隆。
DROP FUNCTION IF EXISTS UC_FIRST;
CREATE FUNCTION UC_FIRST(oldWord VARCHAR(255)) RETURNS VARCHAR(255)
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2));
UC_DELIMITER 在单词之间使用分隔符进行大写
DROP FUNCTION IF EXISTS UC_DELIMITER;
DELIMITER //
CREATE FUNCTION UC_DELIMITER(
oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL
)
RETURNS VARCHAR(255)
BEGIN
SET @oldString := oldName;
SET @newString := "";
tokenLoop: LOOP
IF trimSpaces THEN SET @oldString := TRIM(BOTH " " FROM @oldString); END IF;
SET @splitPoint := LOCATE(delim, @oldString);
IF @splitPoint = 0 THEN
SET @newString := CONCAT(@newString, UC_FIRST(@oldString));
LEAVE tokenLoop;
END IF;
SET @newString := CONCAT(@newString, UC_FIRST(SUBSTRING(@oldString, 1, @splitPoint)));
SET @oldString := SUBSTRING(@oldString, @splitPoint+1);
END LOOP tokenLoop;
RETURN @newString;
END//
DELIMITER ;
<强>示例:强>
SELECT UC_DELIMITER('eric-leroy','-',TRUE);
Eric-Leroy
答案 6 :(得分:0)
Excel中的正确功能(或谷歌工作表完全符合您的要求。
因此,将您的mysql表导出为CSV并导入Excel(或Google工作表)。然后使用= Proper(*text_to_capitalize*)
将每个单词的首字母大写。
然后只需将该Excel表格作为CSV导出回数据库。
答案 7 :(得分:0)
如果您使用的是PHP,那么......
try{
$con = new PDO("mysql:host=localhost;dbname=dbasename", "root", "");
}
catch(PDOException $e){
echo "error" . $e-getMessage();
}
$select = $con->prepare("SELECT * FROM table");
$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
while($data=$select->fetch()) {
$id = $data['id'];
$column = $data['column'];
$column = ucwords(strtolower($column)); // Capitalize each word
$update = $con->prepare("UPDATE table SET column=:column WHERE id='$id'");
$update->bindParam(':column', $column);
$update->execute();
}
答案 8 :(得分:0)
简单地:
SELECT
CONCAT(UCASE(LEFT(firstname, 1)), LCASE(SUBSTRING(firstname, 2))) as firstname
FROM PEOPLE
答案 9 :(得分:0)
如果它是一个计时器,则无需创建函数。以下工作正常:
-- Capitalize first letter of each word in r.name field
SELECT TRIM(CONCAT(
CONCAT(UPPER(SUBSTRING(cname1,1,1)),SUBSTRING(cname1,2)) , " ",
CONCAT(UPPER(SUBSTRING(cname2,1,1)),SUBSTRING(cname2,2)) , " ",
CONCAT(UPPER(SUBSTRING(cname3,1,1)),SUBSTRING(cname3,2))))
FROM (
SELECT
@num_spaces := 1 + LENGTH(c_name) - LENGTH(REPLACE(c_name, ' ', '')) AS
num_spaces,
SUBSTRING_INDEX(CONVERT(c_name,CHAR), ' ', 1) AS cname1,
IF(@num_spaces > 1, SUBSTRING_INDEX(SUBSTRING_INDEX(c_name, ' ', 2), ' ', -1), '') AS cname2,
IF(@num_spaces > 2, SUBSTRING_INDEX(SUBSTRING_INDEX(c_name, ' ', 3), ' ', -1), '') AS cname3
FROM (SELECT (CASE
WHEN UPPER(r.name)COLLATE latin1_general_cs =r.name THEN LOWER(TRIM(r.name))
ELSE TRIM(r.name)
END) AS c_name,r.name
FROM table r) cr) ncr;
注意::IF子句应等于或大于@num_spaces的值。当前的sql将最多处理3个单词。如果需要,您可以添加更多。
答案 10 :(得分:0)
首先创建一个函数
DROP FUNCTION IF EXISTS UC_FIRST;
CREATE FUNCTION UC_FIRST(oldWord VARCHAR(255)) RETURNS VARCHAR(255)
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),SUBSTRING(oldWord, 2));
然后使用此查询
UPDATE mytable SET thefield = UC_FIRST(thefield);
答案 11 :(得分:0)
Eric Leroy 的回答是这里最有趣的回答,但它遗漏了两件事:
这是创建函数的更新代码:
DROP FUNCTION IF EXISTS UC_FIRST;
CREATE FUNCTION UC_FIRST(oldWord VARCHAR(255)) RETURNS VARCHAR(255) DETERMINISTIC
RETURN CONCAT(UCASE(SUBSTRING(oldWord, 1, 1)),LCASE(SUBSTRING(oldWord, 2)));
DROP FUNCTION IF EXISTS UC_DELIMITER;
DELIMITER //
CREATE FUNCTION UC_DELIMITER(
oldName VARCHAR(255), delim VARCHAR(1), trimSpaces BOOL
)
RETURNS VARCHAR(255) DETERMINISTIC
BEGIN
SET @oldString := oldName;
SET @newString := "";
tokenLoop: LOOP
IF trimSpaces THEN SET @oldString := TRIM(BOTH " " FROM @oldString); END IF;
SET @splitPoint := LOCATE(delim, @oldString);
IF @splitPoint = 0 THEN
SET @newString := CONCAT(@newString, UC_FIRST(@oldString));
LEAVE tokenLoop;
END IF;
SET @newString := CONCAT(@newString, UC_FIRST(SUBSTRING(@oldString, 1, @splitPoint)));
SET @oldString := SUBSTRING(@oldString, @splitPoint+1);
END LOOP tokenLoop;
RETURN @newString;
END//
DELIMITER ;
使用以下请求:
SELECT UC_DELIMITER('ThIs IS an ExAMplE oF MIxeD CaseS sTRing',' ',TRUE);
您将获得:
This Is An Example Of Mixed Cases String
答案 12 :(得分:-1)
在phpmyadmin上运行此UPDATE table_name SET Column_Name = LOWER(Column_Name)
然后在显示数据库表中数据的html页面中使用css text-transform: capitalize;