sql select query返回大写

时间:2014-04-03 15:45:56

标签: mysql sql select

mysql的select查询是否有可能返回大小写的字符串?

例如,获取所选查询字段必须以小写形式返回

SELECT LCASE(name) FROM Person;

这将返回

name
-------
mark miguel
john
james
...
顺便说一下,我有一个名为Person的表,而字段name得到了大写的所有值......

name
-------
MARK MIGUEL
JOHN
JAMES
...

我的问题是,是否有一个与LCASE()具有相同功能的select查询只返回大写的?例如,返回的值将如下所示?

name
-------
Mark Miguel
John
James
...

1 个答案:

答案 0 :(得分:1)

使用功能

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(18) DEFAULT ' ()[]{},.-_\'!@;:?/'; -- David Rabby & Lenny Erickson added \'
  SET s = LCASE( str );
  WHILE i <= LENGTH( str ) DO -- Jesse Palmer corrected from < to <= for last char
    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 ;
select proper("d'arcy");
+------------------+
| proper("d'arcy") |
+------------------+
| D'Arcy           |
+------------------+ 

请参阅http://www.artfulsoftware.com/infotree/queries.php#122