0

I'm currently doing a MySQL insert like this:

INSERT INTO RESI (MarketingRemarks) 
SELECT PropertyInformation FROM `property_res`;

I then run independant commands to clean the RESI table of any strange characters like this:

UPDATE RESI SET MarketingRemarks = REPLACE(MarketingRemarks, '“', '"');
UPDATE RESI SET MarketingRemarks = REPLACE(MarketingRemarks, '”', '"');
UPDATE RESI SET MarketingRemarks = REPLACE(MarketingRemarks, '–', '-');
UPDATE RESI SET MarketingRemarks = REPLACE(MarketingRemarks, '½', '1/2');
UPDATE RESI SET MarketingRemarks = REPLACE(MarketingRemarks, '’', '\'');

This seems very inefficient to have all these queries. Is there a way to combine this into a single INSERT statement?

I've tried numerous ways to place the REPLACE into the INSERT statement but with no luck.

Rocco The Taco
  • 3,695
  • 13
  • 46
  • 79

2 Answers2

1

You may use replace function in your select clause, like this:

INSERT INTO RESI (MarketingRemarks) 
SELECT REPLACE(REPLACE(PropertyInformation, '½', '1/2'), '“', '"') FROM `property_res`;

and you need to build chain of your replace function like in example provided by @Arquillian

cn007b
  • 16,596
  • 7
  • 59
  • 74
0

I would suggest to create a function that sanitize the string, and use the function from the insert into command. You'll have an easy readable code.

DELIMITER $$
USE `nestor_dbo`$$
CREATE FUNCTION `Sanitize` (src NVARCHAR(100))
RETURNS NVARCHAR(100)
BEGIN
    DECLARE result NVARCHAR(100);

    SET result = src;

    SET result = replace(result, '“', '"');
    SET result = replace(result, '”', '"');
    SET result = replace(result, '–', '-');
    SET result = replace(result, '½', '1/2');
    SET result = replace(result, '’', '\'');

RETURN result;
END$$


INSERT INTO RESI (MarketingRemarks) 
SELECT Sanitize(PropertyInformation) FROM `property_res`;
DanB
  • 2,022
  • 1
  • 12
  • 24