string - 首字母大写。MySQL的

在MySQL的说法中,有没有人知道这个TSQL的等价物?

我想把每个条目的第一个字母大写。

UPDATE tb_Company SET CompanyIndustry = UPPER(LEFT(CompanyIndustry, 1))
+ SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
Chin asked 2019-08-12T13:56:08Z
13个解决方案
237 votes

它几乎相同,你只需要更改为使用CONCAT()函数而不是+运算符:

UPDATE tb_Company
SET CompanyIndustry = CONCAT(UCASE(LEFT(CompanyIndustry, 1)), 
                             SUBSTRING(CompanyIndustry, 2));

这将转helloHello,wOrLdWOrLd,BLABLABLABLA等。如果你想要大写第一个字母和小写另一个,你只需要使用LCASE函数:

UPDATE tb_Company
SET CompanyIndustry = CONCAT(UCASE(LEFT(CompanyIndustry, 1)), 
                             LCASE(SUBSTRING(CompanyIndustry, 2)));

请注意,UPPER和UCASE做同样的事情。

Vincent Savard answered 2019-08-12T13:56:37Z
38 votes

Vincents对大写第一封信的出色回答非常适用于整个列字符串的首字母大写。

但是,如果你想在表格列的字符串中大写字母的第一个字母怎么办?

例如:"阿比维尔高中"

我还没有在Stackoverflow中找到答案。 我不得不拼凑一些我在谷歌找到的答案,为上面的例子提供了可靠的解决方案。 它不是本机功能,而是用户创建的MySQL版本5+允许的功能。

如果您在MySQL上拥有Super / Admin用户状态或者在您自己的计算机上安装了本地mysql,您可以创建一个FUNCTION(如存储过程),它位于您的数据库中,并且可以在以后的所有SQL查询中使用。 D b。

我创建的函数允许我使用这个我称之为" UC_Words" 就像MySQL的内置本机函数一样,这样我就可以像这样更新一个完整的列:

UPDATE Table_name
SET column_name = UC_Words(column_name) 

要插入功能代码,我在创建函数时更改了MySQL标准分隔符(;),然后在函数创建脚本之后将其重置为正常。 我个人也希望输出也是UTF8 CHARSET。

功能创建=

DELIMITER ||  

CREATE FUNCTION `UC_Words`( str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET utf8_general_ci  
BEGIN  
  DECLARE c CHAR(1);  
  DECLARE s VARCHAR(255);  
  DECLARE i INT DEFAULT 1;  
  DECLARE bool INT DEFAULT 1;  
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';  
  SET s = LCASE( str );  
  WHILE i < LENGTH( str ) DO  
     BEGIN  
       SET c = SUBSTRING( s, i, 1 );  
       IF LOCATE( c, punct ) > 0 THEN  
        SET bool = 1;  
      ELSEIF bool=1 THEN  
        BEGIN  
          IF c >= 'a' AND c <= 'z' THEN  
             BEGIN  
               SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));  
               SET bool = 0;  
             END;  
           ELSEIF c >= '0' AND c <= '9' THEN  
            SET bool = 0;  
          END IF;  
        END;  
      END IF;  
      SET i = i+1;  
    END;  
  END WHILE;  
  RETURN s;  
END ||  

DELIMITER ; 

这适用于输出字符串中多个单词的大写首字母。

假设您的MySQL登录用户名具有足够的权限 - 如果没有,并且您无法在个人计算机上设置临时数据库来转换表格,请询问您的共享主机提供商是否为您设置此功能。

Martin Sansone - MiOEE answered 2019-08-12T13:58:24Z
17 votes

您可以使用UCASE(),MID()CONCAT()的组合:

SELECT CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS name FROM names;
Wouter Dorgelo answered 2019-08-12T13:58:52Z
8 votes
mysql> SELECT schedule_type AS Schedule FROM ad_campaign limit 1;
+----------+
| Schedule |
+----------+
| ENDDATE  |
+----------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT(UCASE(MID(schedule_type,1,1)),LCASE(MID(schedule_type,2))) AS Schedule FROM ad_campaign limit 1;
+----------+
| Schedule |
+----------+
| Enddate  |
+----------+
1 row in set (0.00 sec)

[http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_mid]

Root answered 2019-08-12T13:59:25Z
5 votes

[http://forge.mysql.com/tools/tool.php?id=201]

如果列中有多个单词,则此操作将不起作用,如下所示。在这种情况下,上面提到的UDF可能有所帮助。

mysql> select * from names;
+--------------+
| name         |
+--------------+
| john abraham | 
+--------------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS name FROM names;
+--------------+
| name         |
+--------------+
| John abraham | 
+--------------+
1 row in set (0.00 sec)

或许这个会有所帮助......

[https://github.com/mysqludf/lib_mysqludf_str#str_ucwords]

shantanuo answered 2019-08-12T14:00:20Z
2 votes

这很好用。

UPDATE状态SET name = CONCAT(UCASE(LEFT(name,1)),                                LCASE(SUBSTRING(name,2)));

Abhinav Sahu answered 2019-08-12T14:00:55Z
1 votes
UPDATE tb_Company SET CompanyIndustry = UCASE(LEFT(CompanyIndustry, 1)) + 
SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
Jasdeep Singh answered 2019-08-12T14:01:14Z
1 votes

创造一个功能:

CREATE DEFINER=`root`@`localhost` FUNCTION `UC_FIRST`(`oldWord` VARCHAR(255)) 

RETURNS varchar(255) CHARSET utf8

RETURN CONCAT( UCASE( LEFT(oldWord, 1)), LCASE(SUBSTRING(oldWord, 2)))

使用功能

UPDATE tbl_name SET col_name = UC_FIRST(col_name);
Florin answered 2019-08-12T14:01:49Z
1 votes

如果有人试图将每个单词用空格分开...

CREATE FUNCTION response(name VARCHAR(40)) RETURNS VARCHAR(200) DETERMINISTIC
BEGIN
   set @m='';
   set @c=0;
   set @l=1;
   while @c <= char_length(name)-char_length(replace(name,' ','')) do
      set @c = @c+1;
      set @p = SUBSTRING_INDEX(name,' ',@c);
      set @k = substring(name,@l,char_length(@p)-@l+1);
      set @l = char_length(@k)+2;
      set @m = concat(@m,ucase(left(@k,1)),lcase(substring(@k,2)),' ');
   end while;
   return trim(@m); 
END;
CREATE PROCEDURE updateNames()
BEGIN
  SELECT response(name) AS name FROM names;
END;

结果

+--------------+
| name         |
+--------------+
| Abdul Karim  | 
+--------------+
Jahir islam answered 2019-08-12T14:02:19Z
0 votes

这应该很好地工作:

UPDATE tb_Company SET CompanyIndustry = 
CONCAT(UPPER(LEFT(CompanyIndustry, 1)), SUBSTRING(CompanyIndustry, 2))
Chris Hutchinson answered 2019-08-12T14:02:59Z
0 votes
UPDATE users
SET first_name = CONCAT(UCASE(LEFT(first_name, 1)), 
                             LCASE(SUBSTRING(first_name, 2)))
,last_name = CONCAT(UCASE(LEFT(last_name, 1)), 
                             LCASE(SUBSTRING(last_name, 2)));
sandeep kumar answered 2019-08-12T14:03:20Z
0 votes
 select  CONCAT(UCASE(LEFT('CHRIS', 1)),SUBSTRING(lower('CHRIS'),2));

上述声明可用于首字母CAPS,其余为小写。

Aamir Khan answered 2019-08-12T14:03:50Z
-1 votes

Uso algo simples assim;)

DELIMITER $$
DROP FUNCTION IF EXISTS `uc_frist` $$
CREATE FUNCTION `uc_frist` (str VARCHAR(200)) RETURNS varchar(200)
BEGIN
    set str:= lcase(str);
    set str:= CONCAT(UCASE(LEFT(str, 1)),SUBSTRING(str, 2));
    set str:= REPLACE(str, ' a', ' A');
    set str:= REPLACE(str, ' b', ' B');
    set str:= REPLACE(str, ' c', ' C');
    set str:= REPLACE(str, ' d', ' D');
    set str:= REPLACE(str, ' e', ' E');
    set str:= REPLACE(str, ' f', ' F');
    set str:= REPLACE(str, ' g', ' G');
    set str:= REPLACE(str, ' h', ' H');
    set str:= REPLACE(str, ' i', ' I');
    set str:= REPLACE(str, ' j', ' J');
    set str:= REPLACE(str, ' k', ' K');
    set str:= REPLACE(str, ' l', ' L');
    set str:= REPLACE(str, ' m', ' M');
    set str:= REPLACE(str, ' n', ' N');
    set str:= REPLACE(str, ' o', ' O');
    set str:= REPLACE(str, ' p', ' P');
    set str:= REPLACE(str, ' q', ' Q');
    set str:= REPLACE(str, ' r', ' R');
    set str:= REPLACE(str, ' s', ' S');
    set str:= REPLACE(str, ' t', ' T');
    set str:= REPLACE(str, ' u', ' U');
    set str:= REPLACE(str, ' v', ' V');
    set str:= REPLACE(str, ' w', ' W');
    set str:= REPLACE(str, ' x', ' X');
    set str:= REPLACE(str, ' y', ' Y');
    set str:= REPLACE(str, ' z', ' Z');
    return  str;
END $$
DELIMITER ;
Hton answered 2019-08-12T14:04:20Z
translate from https://stackoverflow.com:/questions/4263272/capitalize-first-letter-mysql