如何对mysql列进行“正确的大小写”格式化?

时间:2011-05-31 00:54:43

标签: mysql

在mysql中是否可以在Proper Case中格式化列?

示例:Proper("ABSALOM") = "Absalom"

我已经搜索了很多,我认为MySQL没有任何内置功能来执行此操作。是否有可能在MySQL中以任何其他方式执行此操作?

5 个答案:

答案 0 :(得分:40)

您可以结合使用CONCAT和SUBSTRING:

CONCAT(UCASE(SUBSTRING(`fieldName`, 1, 1)),LOWER(SUBSTRING(`fieldName`, 2)))

答案 1 :(得分:11)

你会认为世界上最流行的开源数据库,就像MySQL喜欢称之为自己一样,会有一个函数来制作项目标题大小写(每个单词的第一个字母大写)。可悲的是,它没有。

这是我发现的最佳解决方案只需创建一个可以解决问题的存储过程/函数

mysql> 
DROP FUNCTION IF EXISTS proper;
SET GLOBAL  log_bin_trust_function_creators=TRUE;
DELIMITER |
CREATE FUNCTION proper( str VARCHAR(128) )
RETURNS VARCHAR(128)
BEGIN
DECLARE c CHAR(1);
DECLARE s VARCHAR(128);
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 ;

然后

update table set col = proper(col)

select proper( col ) as properCOl 
from table 

多田 欢迎您

答案 2 :(得分:1)

-- set upper case the first char of every word in a string
DROP FUNCTION IF EXISTS PROPER;
DELIMITER $$
CREATE FUNCTION PROPER(inputStr varchar(1500)) 
RETURNS VARCHAR(1500)
DETERMINISTIC
BEGIN
    DECLARE x, y, result VARCHAR(1500);
    SET result = '';
    SET x = '';
    SET y = LOWER(TRIM(inputStr));
    WHILE CHAR_LENGTH(y) > 0 DO
        -- get next word
        SET x = SUBSTRING_INDEX(y, ' ', 1);
        -- set upper case the first char
        SET x = CONCAT(UPPER(SUBSTRING(x, 1, 1)), SUBSTRING(x, 2));
        -- the final s (greek language)               
        IF RIGHT(x,1) = 'σ' THEN
            SET x = CONCAT(left(x, CHAR_LENGTH(x) - 1),'ς');
        END IF;
        -- add word to result
        SET result = CONCAT(result, ' ', x);
        -- prepare y for next loop
        SET y = TRIM(SUBSTRING(y, CHAR_LENGTH(x) + 1));
    END WHILE;
    RETURN (TRIM(result));
END$$
DELIMITER;

答案 3 :(得分:0)

@Pascal的解决方案适用于拉丁字符。任何干预不同排序规则都会使事情变得混乱。

我认为@Pascal的真正含义更像是这样:

DELIMITER |

CREATE or replace FUNCTION func_proper( str VARCHAR(255) )
RETURNS VARCHAR(255)
BEGIN
  DECLARE chr VARCHAR(1);
  DECLARE lStr VARCHAR(255);
  DECLARE oStr VARCHAR(255) DEFAULT '';
  DECLARE i INT DEFAULT 1;
  DECLARE bool INT DEFAULT 1;
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';

  WHILE i <= LENGTH( str ) DO
    BEGIN
      SET chr = SUBSTRING( str, i, 1 );
      IF LOCATE( chr, punct ) > 0 THEN
        BEGIN
          SET bool = 1;
          SET oStr = concat(oStr, chr);
        END;
      ELSEIF bool=1 THEN
        BEGIN
          SET oStr = concat(oStr, UCASE(chr));
          SET bool = 0;
        END;
      ELSE
        BEGIN
          SET oStr = concat(oStr, LCASE(chr));
        END;
      END IF;
      SET i = i+1;
    END;
  END WHILE;

  RETURN oStr;
END;

|
DELIMITER ;

答案 4 :(得分:0)

如果单列中只有单词,即。名字和姓氏。 我们可以连接子字符串。

select customer_name, concat(   
  upper(substring(substring_index(customer_name,' ',1),1,1)),   
  lower(substring(substring_index(customer_name,' ',1),2)) , ' ',
  upper(substring(substring_index(customer_name,' ',-1),1,1)),
  lower(substring(substring_index(customer_name,' ',-1),2)) 
) from customer;