Use of the replace () function of MySQL

Source: Internet
Author: User
Tags mysql manual
I encountered a problem during my work today. I had to replace the shop.xxxx.net characters in all values of a column in the database with www.nowamagic.net. I could have written a script, all the values are extracted and processed using php, but the efficiency is very low. I want to see if I can directly process them using SQL statements in MySQL.

When I encountered a problem during my work today, I had to replace the "shop.xxxx.net" character in all the values of a column in the database with "www.nowamagic.net". I could have written a script, all the values are extracted and processed using php, but the efficiency is very low. I want to see if I can directly process them using SQL statements in MySQL.

When I encountered a problem during my work today, I had to replace the "shop.xxxx.net" character in all the values of a column in the database with "www.nowamagic.net". I could have written a script, all the values are extracted and processed using php, but the efficiency is very low. I want to see if I can directly process them using SQL statements in MySQL. After some searches, finally, I found a solution. In fact, the most important thing is the replace function of mysql. I did not understand this function in the MySQL manual, however, you only need to implement the functions you want.

The following is a brief introduction and example of this function.

For example, you want to replace the abc of the f1 field in Table tb1 with def:

UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def');REPLACE(str,from_str,to_str)

All the from_str strings in the str string are replaced by to_str, and the following string is returned:

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

This function is multi-byte secure.

Example:

UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'', "); UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'', "); UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'', "); UPDATE 'dede _ archives' SET title = REPLACE (title, 'concise modern magic -',"); UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'.. /.. /.. /.. /.. /.. /', 'HTTP: // special.dayoo.com/meal /');
Mysql replace

Usage 1. replace intoreplace into table (id, name) values ('1', 'A'), ('2', 'bb ')

This statement inserts two records into the table.

2. replace (object, search, replace)

Replace all search objects with replaceselect replace ('www .163.com ', 'w', 'ww')-> www wWw.163.com

For example, replace aa in the name field of the table with bbupdate table set name = replace (name, 'AA', 'bb ')

Replace text or ntext fields in SQL Server

At the beginning, the Update AA table Set xx field = Replace (xx field, "to Replace", "specific string") encountered an error: the data type ntext of parameter 1 of the replace function is invalid. Update article set heading = Replace (convert (nvarchar (4000), heading), 'script, script ', '')

Update table name set text field name = replace (convert (varchar (8000), text field name), 'replacement character ', 'replacement value ')

Varchar and nvarchar support replace, so if your text/ntext cannot exceed 8000/4000, you can convert it to the first two types before using replace.

Update table name set text field name = replace (convert (varchar (8000), text field name), 'replacement character ', 'replacement value ')
Update table name set ntext field name = replace (convert (nvarchar (4000), ntext field name), 'replacement characters', 'replacement to value ')

If text/ntext exceeds 8000/4000, see the following example:

Declare @ pos int declare @ len int declare @ str nvarchar (4000) declare @ des nvarchar (4000) declare @ count int set @ des ='
 
  
'-- Set @ len = len (@ des) set @ str ='
  
   
'-- The character set @ count = 0 to be replaced -- count. WHILE 1 = 1 BEGIN select @ pos = patINDEX ('%' + @ des + '%', propxmldata) -1 from table name where condition IF @ pos> = 0 begin DECLARE @ ptrval binary (16) SELECT @ ptrval = TEXTPTR (field name) from table name where condition UPDATETEXT table name. field name @ ptrval @ pos @ len @ str set @ count = @ count + 1 end ELSE break; END select @ count
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.