MySQL replace () function introduction bitsCN.com
MySQL replace () function introduction
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:
1
UPDATE tb1 SET f1 = REPLACE (f1, 'ABC', 'Def ');
2
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:
1
Mysql> select replace ('www .mysql.com ', 'W', 'WW ');
2
-> 'Wwwwww .mysql.com'
This function is multi-byte secure.
Example:
1
UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'',");
2
UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'',");
3
UPDATE 'dede _ addonarticle' SET body = REPLACE (body ,'',");
4
UPDATE 'dede _ archives 'SET title = REPLACE (title, 'Concise modern magic -',");
5
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 ', '')
1
Update table name
2
Set text field name = replace (convert (varchar (8000), text field name), 'replacement character ', 'Replacement to 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.
1
Update table name
2
Set text field name = replace (convert (varchar (8000), text field name), 'replacement character ', 'Replacement to value ')
1
Update table name
2
Set ntext field name = replace (convert (nvarchar (4000), ntext field name), 'replacement character ', 'replacement value ')
If text/ntext exceeds 8000/4000, see the following example:
01
Declare @ pos int
02
Declare @ len int
03
Declare @ str nvarchar (4000)
04
Declare @ des nvarchar (4000)
05
Declare @ count int
06
Set @ des =' '-- Value to be replaced
07
08
Set @ len = len (@ des)
09
Set @ str =' '-- The character to be replaced
10
11
12
Set @ count = 0 -- count.
13
14
15
WHILE 1 = 1
16
BEGIN
17
Select @ pos = patINDEX ('%' + @ des + '%', propxmldata)-1
18
From table name
19
Where condition
20
21
IF @ pos> = 0
22
Begin
23
DECLARE @ ptrval binary (16)
24
SELECT @ ptrval = TEXTPTR (field name)
25
From table name
26
Where condition
27
UPDATETEXT table name. field name @ ptrval @ pos @ len @ str
28
Set @ count = @ count + 1
29
End
30
ELSE
31
Break;
32
END
33
34
Select @ count
BitsCN.com