Raj
Raj

Reputation: 41

Use Replace Clause in Insert Stataement for MYSQL

I am using MySQL database. I want to insert data into it. But one column data contains special characters. (backslash) I want to replace it with Double backslash and then execute Insert query.

Can we do it using Insert Query?

While looking for the answer I came across

UPDATE your_table
SET your_field = REPLACE(your_field, '/', '//')
WHERE your_field LIKE '%articles/updates/%'

So it is possible use Replace in Update query.

Can we do the same in insert query? Please Let me know if you can help me.

Upvotes: 1

Views: 52

Answers (2)

Adel
Adel

Reputation: 1468

You can use the following:

REPLACE INTO table_name(column_name1,column_name2,…)
VALUES(value1,value2,…)

More info from:

http://www.mysqltutorial.org/mysql-replace.aspx

Upvotes: 1

Deepak Rai
Deepak Rai

Reputation: 2203

You can use a BEFORE INSERT TRIGGER on your table

CREATE TRIGGER trigger_name
BEFORE INSERT 
ON my_table
FOR EACH ROW
SET NEW.col_name = IF(NEW.col_name = '/','//',NEW.col_name);

Upvotes: 0

Related Questions