简体   繁体   中英

MySQL equivalent of Oracle's SUBSTITUTE function - Space replace issue

I have created this small function(Similar to oracle's substitute function), it works fine for most characters but I can't figure out why it does not replace spaces...

Can somebody throw some light on this please?

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 ;

In MySql there already is a REPLACE function :

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

isn't this what you want ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM