第一个字母大写。 MySQL

发布于 2024-10-04 11:28:04 字数 211 浏览 16 评论 0原文

有谁知道 MySQL 用语中 TSQL 的等价物吗?

我试图将每个条目的第一个字母大写。

UPDATE tb_Company SET CompanyIndustry = UPPER(LEFT(CompanyIndustry, 1))
+ SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))

Does any one know the equivalent to this TSQL in MySQL parlance?

I am trying to capitalize the first letter of each entry.

UPDATE tb_Company SET CompanyIndustry = UPPER(LEFT(CompanyIndustry, 1))
+ SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

妖妓 2024-10-11 11:28:04

几乎是一样的,您只需更改为使用 CONCAT() 函数而不是 + 运算符:

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

这会将 hello 变为 HellowOrLd 到 WOrLdBLABLABLABLA 等。如果您想将第一个字母大写,另一个字母小写,您可以只需使用 LCASE 函数:

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

请注意,UPPER 和 UCASE 执行相同的操作。

It's almost the same, you just have to change to use the CONCAT() function instead of the + operator :

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

This would turn hello to Hello, wOrLd to WOrLd, BLABLA to BLABLA, etc. If you want to upper-case the first letter and lower-case the other, you just have to use LCASE function :

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

Note that UPPER and UCASE do the same thing.

水水月牙 2024-10-11 11:28:04

Vincents 对大写第一个字母的出色回答非常适合整个列字符串的第一个字母大写。

但是如果您想将第一个字母大写怎么办表列字符串中的每个单词

例如:“Abbeville High School”

我在 Stackoverflow 中没有找到这个问题的答案。我必须拼凑在谷歌中找到的一些答案,才能为上述示例提供可靠的解决方案。它不是本机函数,而是 MySQL 5+ 版本允许的用户创建的函数。

如果您在 MySQL 上具有超级/管理员用户状态,或者在您自己的计算机上安装了本地 mysql,您可以创建一个函数(如存储过程),该函数位于您的数据库中,并且可以在未来的任何部分的所有 SQL 查询中使用。数据库。

我创建的函数允许我使用这个我称为“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 DETERMINISTIC  
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 登录用户名有足够的权限 - 如果没有,并且您无法在您的个人计算机上设置临时数据库来转换您的表,那么请询问您的共享托管提供商是否会为您设置此功能。

Vincents excellent answer for Uppercase First Letter works great for the first letter only capitalization of an entire column string..

BUT what if you want to Uppercase the First Letter of EVERY word in the strings of a table column?

eg: "Abbeville High School"

I hadn't found an answer to this in Stackoverflow. I had to cobble together a few answers I found in Google to provide a solid solution to the above example. Its not a native function but a user created function which MySQL version 5+ allows.

If you have Super/Admin user status on MySQL or have a local mysql installation on your own computer you can create a FUNCTION (like a stored procedure) which sits in your database and can be used in all future SQL query on any part of the db.

The function I created allows me to use this new function I called "UC_Words" just like the built in native functions of MySQL so that I can update a complete column like this:

UPDATE Table_name
SET column_name = UC_Words(column_name) 

To insert the function code, I changed the MySQL standard delimiter(;) whilst creating the function, and then reset it back to normal after the function creation script. I also personally wanted the output to be in UTF8 CHARSET too.

Function creation =

DELIMITER ||  

CREATE FUNCTION `UC_Words`( str VARCHAR(255) ) RETURNS VARCHAR(255) CHARSET utf8 DETERMINISTIC  
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 ; 

This works a treat outputting Uppercase first letters on multiple words within a string.

Assuming your MySQL login username has sufficient privileges - if not, and you cant set up a temporary DB on your personal machine to convert your tables, then ask your shared hosting provider if they will set this function for you.

从此见与不见 2024-10-11 11:28:04

您可以结合使用 UCASE()MID()CONCAT()

SELECT CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS name FROM names;

You can use a combination of UCASE(), MID() and CONCAT():

SELECT CONCAT(UCASE(MID(name,1,1)),MID(name,2)) AS name FROM names;
戏舞 2024-10-11 11:28:04
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

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

仙女 2024-10-11 11:28:04

这工作得很好。

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

This is working nicely.

UPDATE state SET name = CONCAT(UCASE(LEFT(name, 1)), LCASE(SUBSTRING(name, 2)));
等风来 2024-10-11 11:28:04

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

如果该列中有超过 1 个单词,那么这将不起作用,如下所示。
上面提到的 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

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

If there are more than 1 word in the column, then this will not work as shown below.
The UDF mentioned above may help in such case.

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)

Or maybe this one will help...

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

明月夜 2024-10-11 11:28:04
UPDATE tb_Company SET CompanyIndustry = UCASE(LEFT(CompanyIndustry, 1)) + 
SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
UPDATE tb_Company SET CompanyIndustry = UCASE(LEFT(CompanyIndustry, 1)) + 
SUBSTRING(CompanyIndustry, 2, LEN(CompanyIndustry))
画▽骨i 2024-10-11 11:28:04

创建函数:

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

CREATE A FUNCTION:

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

USE THE FUNCTION

UPDATE tbl_name SET col_name = UC_FIRST(col_name);
千年*琉璃梦 2024-10-11 11:28:04

如果有人尝试将每个单词大写并用空格分隔...

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  | 
+--------------+

If anyone try to capitalize the every word separate by space...

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;

Result

+--------------+
| name         |
+--------------+
| Abdul Karim  | 
+--------------+
七度光 2024-10-11 11:28:04

这应该可以很好地工作:

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

This should work nicely:

UPDATE tb_Company SET CompanyIndustry = 
CONCAT(UPPER(LEFT(CompanyIndustry, 1)), SUBSTRING(CompanyIndustry, 2))
过度放纵 2024-10-11 11:28:04
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)));
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)));
兔姬 2024-10-11 11:28:04
 select  CONCAT(UCASE(LEFT('CHRIS', 1)),SUBSTRING(lower('CHRIS'),2));

上述语句首字母大写,其余字母小写。

 select  CONCAT(UCASE(LEFT('CHRIS', 1)),SUBSTRING(lower('CHRIS'),2));

Above statement can be used for first letter CAPS and rest as lower case.

寂寞笑我太脆弱 2024-10-11 11:28:04

PostgreSQL 中的解决方案(就谷歌搜索可能会导致此页面)

INITCAP(firstname || ' ' || lastname) AS fullname

The solution in PostgreSQL (as far as googling may lead to this page)

INITCAP(firstname || ' ' || lastname) AS fullname
感受沵的脚步 2024-10-11 11:28:04

Uso 算法简化了 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 ;

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 ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文