要求是根据美国英语词典
替换列值中的区分大小写的文本以下是样本
Colour => color
Color => Color (note that C is capitalized here)
FIBRE => FIBER
Colour/Monochrome => Color/Monochrome
答案 0 :(得分:1)
此功能声称可以使用,但我没有选中,但在another question用户接受此作为回答
http://pento.net/2009/02/15/case-insensitive-replace-for-mysql/
如果这不起作用,请告诉我
答案 1 :(得分:0)
试试这个:
DELIMITER $$
DROP FUNCTION IF EXISTS `replace_ci`$$
CREATE FUNCTION `replace_ci` (str TEXT, needle CHAR(255), str_rep CHAR(255))
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE return_str TEXT DEFAULT '';
DECLARE lower_str TEXT;
DECLARE lower_needle TEXT;
DECLARE pos INT DEFAULT 1;
DECLARE old_pos INT DEFAULT 1;
SELECT lower(str) INTO lower_str;
SELECT lower(needle) INTO lower_needle;
SELECT locate(lower_needle, lower_str, pos) INTO pos;
WHILE pos > 0 DO
SELECT concat(return_str, substr(str, old_pos, pos-old_pos), str_rep) INTO return_str;
SELECT pos + char_length(needle) INTO pos;
SELECT pos INTO old_pos;
SELECT locate(lower_needle, lower_str, pos) INTO pos;
END WHILE;
SELECT concat(return_str, substr(str, old_pos, char_length(str))) INTO return_str;
/** mirror the case **/
IF (BINARY LEFT(str,1) = LOWER(LEFT(str,1))) THEN
SET return_str = LOWER(return_str);
ELSE
IF (BINARY LEFT(str,2) = UPPER(LEFT(str,2))) THEN
SET return_str = UPPER(return_str);
ELSE
SET return_str = CONCAT( UPPER(LEFT(return_str,1)), LOWER(RIGHT(return_str, LENGTH(return_str) - 1)) );
END IF;
END IF;
RETURN return_str;
END$$
DELIMITER ;
我已将不区分大小写的替换功能(在其他答案中提到)修改为镜像'原始字符串的情况
(参见/ **镜像后的代码部分** /)。
但是,它只检查3个场景 - 输入字符串是
但它应该适用于这三种情况 - 即
FIBER =>纤维
Fiber =>纤维
fiber =>纤维