【问题标题】:MySQL equivalent of Oracle's SUBSTITUTE function - Space replace issueMySQL 等效于 Oracle 的 SUBSTITUTE 函数 - 空格替换问题
【发布时间】:2012-08-12 13:05:52
【问题描述】:

我创建了这个小函数(类似于 oracle 的替换函数),它适用于大多数字符,但我不知道为什么它不能替换空格...

有人可以解释一下吗?

DROP FUNCTION IF EXISTS MPT_FUNC_SUBSTITUTE;
DELIMITER // 
CREATE FUNCTION MPT_FUNC_SUBSTITUTE
(
  p_in_str          VARCHAR(1000),
  p_substitute_from VARCHAR(100),
  p_substitute_to   VARCHAR(100)
) 
RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
#--+==============================================================================+
#--| Name        : MPT_FUNC_SUBSTITUTE                                            |
#--|                                                                              |
#--| Description : Designed to search for platinum ads.                           |
#--|                                                                              |
#--| Parameters  : p_in_str          [Mandatory] - The string to modify.          |
#--|               p_substitute_from [Mandatory] - Susbtitute from string         |
#--|               p_substitute_to   [Mandatory] - Susbtitute to string           |
#--|                                                                              |
#--| Returns     : p_out_str                                                      |
#--|                                                                              |
#--| Author             Date        Version   Remarks                             |
#--| ------------------ ----------- --------- ----------------------------------- |
#--| Leo Coppens        12-Aug-2012 1.0       Created                             |
#--|                                                                              |
#--+==============================================================================+

#-- DECLARE statements
  DECLARE lc_api_name   VARCHAR(30)   DEFAULT 'MPT_PROC_SEARCHDEALERS';
  DECLARE i             INT           DEFAULT 1;
  DECLARE chr1,chr2     CHAR(1);
  DECLARE p_out_str     VARCHAR(1000) DEFAULT p_in_str;

#-- Program Logic

  #-- Do the replacement if the necessary values are provided
  IF     p_in_str          IS NOT NULL
     AND p_substitute_from IS NOT NULL
  THEN
    SET p_out_str = p_in_str;
    #-- Start the replacement loop
    WHILE i <= CHAR_LENGTH(p_substitute_from) 
    DO
      #-- Get the characters to replace from and to
      SET chr1 = SUBSTR(p_substitute_from, i, 1);
      SET chr2 = IFNULL(SUBSTR(p_substitute_to, i, 1),'');
      #-- Do the replacement now
      SET p_out_str = REPLACE(p_out_str, chr1, chr2);
      #-- Increase i to continue the loop
      SET i = i + 1;
    END WHILE; 

  RETURN p_out_str;
  END IF;

END //

DELIMITER ;

【问题讨论】:

    标签: mysql function substitution


    【解决方案1】:

    MySql 中已经有一个 REPLACE 函数:

    SELECT REPLACE('www.mysql.com', 'w', 'Ww');
    

    这不是你想要的吗?

    【讨论】:

    • 嗨,dyyyy,这使用替换,但对于多个字符,不只是一次作为普通替换......但由于某种原因它不占用空间
    猜你喜欢
    • 2015-11-22
    • 2012-12-27
    • 1970-01-01
    • 2017-04-26
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多