Mysql - 找到camelcase时的拆分结果

时间:2016-01-04 02:50:59

标签: mysql

我的表有一个名为name的列,包含camelCase格式的人名,但是当找到大写字母时,我需要选择一个空格分隔的名称:例如,我的表格如下:

|name          |
----------------
| JosephJackson|
|AidenChase    |
|LukeBenjamin  |

但我希望以这种方式:

 |name          |
 ----------------
 | Joseph Jackson|
 |Aiden Chase    |
 |Luke Benjamin  |

我该如何使用我的SELECT?感谢

3 个答案:

答案 0 :(得分:2)



BEGIN
SET @text = word;
SET @result = "";
SET @i = 1;
WHILE @i <= LENGTH(@text) DO
SET @t = SUBSTRING(@text, @i, 1);
IF @i > 1 AND ASCII(SUBSTRING(@text, @i, 1)) BETWEEN 65 AND 90  OR @t ='_' OR @t = '-' THEN
IF @t <> '_' AND @t <> '-' THEN
SET @result = CONCAT(@result,' ');
END IF;
END IF;
IF @t <> '_' AND @t <> '-' THEN
SET @result = CONCAT(@result , @t);
end IF;
SET @i = @i + 1;
END WHILE;
RETURN @result;
END
&#13;
&#13;
&#13;

上面的sql函数会将文本从驼峰大小写中分离出来,如果在该文本中使用它们,也会从下划线(_)和短划线( - )等特殊字符中分离出来。

答案 1 :(得分:1)

我根据@ Mahesh的答案改编了我想要完成的事情(个人喜好):

  1. 将第一个字符设为大写
  2. 制作-_个字符
  3. 句柄缩写
  4. 用小写字母替换小字
  5. 这是两者的比较。

    <强>矿:

    SELECT camelCaseToSpaced('iWasUsingThisForSetting-name-conversionForTheACMECo_project');
    

    ...输出

    I Was Using This for Setting name conversion for the ACME Co project
    

    Mahesh的版本

    SELECT camelCaseToSpacedMahesh('iWasUsingThisForSetting-name-conversionForTheACMECo_project');
    

    ...输出

    i Was Using This For Settingnameconversion For The A C M E Coproject
    

    所以这里我的代码

    CREATE FUNCTION camelCaseToSpaced(string VARCHAR(100)) RETURNS VARCHAR(255)
      BEGIN
        SET @text = string;
        SET @result = "";
        SET @i = 1;
        SET @caps = 0;
    
        WHILE @i <= LENGTH(@text) DO
          SET @t = SUBSTRING(@text, @i, 1);
          SET @lastCaps = @caps;
          SET @caps = 0;
          -- uppers or _ or -
          IF @i > 1 AND ASCII(SUBSTRING(@text, @i, 1)) BETWEEN 65 AND 90 OR @t ='_' OR @t = '-' THEN
            IF NOT (@t ='_' OR @t = '-') THEN
              set @caps = 1;
            ELSE
              set @caps = 0;
            END IF;
            IF @i < LENGTH(@text) THEN
              set @nextAscii = ASCII(SUBSTRING(@text, @i+1, 1));
            END IF;
            SET @sep = ' ';
            -- to not have no space separator we need:
            -- 1. previous char is cap
            -- 2. next ascii is cap
            IF (@lastCaps = 1 AND @nextAscii BETWEEN 65 AND 90) THEN
              SET @sep = '';
            END IF;
            SET @result = CONCAT(@result, @sep);
          END IF;
          -- all characters except _ and -
          IF @t <> '_' AND @t <> '-' THEN
            -- make first character uppercase
            IF @i = 1 THEN
              SET @t = UPPER(@t);
            END IF;
            SET @result = CONCAT(@result , @t);
          end IF;
          SET @i = @i + 1;
        END WHILE;
    
        SET @result = replace(@result, ' The ', ' the ');
        SET @result = replace(@result, ' As ', ' as ');
        SET @result = replace(@result, ' In ', ' in ');
        SET @result = replace(@result, ' To ', ' to ');
        SET @result = replace(@result, ' On ', ' on ');
        SET @result = replace(@result, ' Of ', ' of ');
        SET @result = replace(@result, ' For ', ' for ');
    
        RETURN @result;
      END;
    

答案 2 :(得分:0)

我对@MaheshYadav代码做了一些修饰和更新。这样会将camelCase字符串更改为大写。

DELIMITER @@

DROP FUNCTION IF EXISTS change_case @@
CREATE FUNCTION change_case (word CHAR(255)) RETURNS VARCHAR(255)
COMMENT 'Change the case to Proper Case, 65-90 (decimal|ASCII) are A-Z, but if first char in between 97-122 than change case to upper' DETERMINISTIC
BEGIN
  SET @text = word;
  SET @result = "";
  SET @i = 1;
  IF ASCII(SUBSTRING(@text, 1, 1)) BETWEEN 97 AND 122 THEN
    SET @text = CONCAT(UPPER(SUBSTRING(@text, 1, 1)), SUBSTRING(@text, 2));
  END IF;
  WHILE @i <= LENGTH(@text) DO
    SET @t = SUBSTRING(@text, @i, 1);
    IF @i > 1 AND ASCII(SUBSTRING(@text, @i, 1)) BETWEEN 65 AND 90  OR @t ='_' OR @t = '-' THEN
      IF @t <> '_' AND @t <> '-' THEN
        SET @result = CONCAT(@result,' ');
      END IF;
    END IF;
    IF @t <> '_' AND @t <> '-' THEN
      SET @result = CONCAT(@result , @t);
    END IF;
    SET @i = @i + 1;
  END WHILE;
  RETURN @result;
END @@

DELIMITER ;

示例用法:

UPDATE users SET `name` = change_case(userName);