当前位置:网站首页>Replace() function
Replace() function
2022-07-04 10:57:00 【ths512】
grammar
REPLACE ( string_expression , string_pattern , string_replacement )
Parameters
string_expression String expression to search .string_expression It can be character or binary data type .
string_pattern Is the substring to find .string_pattern It can be character or binary data type .string_pattern It cannot be an empty string ('').
string_replacement Replace string .string_replacement It can be character or binary data type .
Return type
If the data type of one of the input parameters is nvarchar, Then return to nvarchar; otherwise REPLACE return varchar.
If any parameter is NULL, Then return to NULL.
It's all Mandarin , Not easy to understand ! Turn into vernacular :
REPLACE(String,from_str,to_str) namely : take String All of the from_str Replace with to_str.
One 、 Prepare the experimental environment
1.1 Create table :
CREATE TABLE `test_tb` (
`id` int(10) unsigned NOT NULL auto_increment COMMENT ' The primary key increases automatically ',
`name` char(30) default NULL COMMENT ' full name ',
`address` char(60) default NULL COMMENT ' Address ',
`country` char(200) default NULL COMMENT ' Country ',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT=' The test table '
1.2 insert data :
insert into test_tb(name,address,country) values
('zhangsan',' Beijing Chaoyang District ',' China '),
('lisi',' Shanghai Pudong district ',' China '),
('wangwu',' Zhengzhou Jinshui District ',' China '),
('zhaoliu',' Kowloon, Hong Kong ',' Hong Kong, China, '),
('Q7',' California beef ',' The United States '),
('wangba',' New Kyushu Island ',' Japan ')
Two 、 Search for replacement
2.1 take address In the field “ District ” Replace with “ Vomit ” Show , as follows
select *,replace(address,' District ',' Vomit ') AS rep from test_tb
2.2 take address In the field “ Nine ” Replace with “ Ten ” Show , as follows
select *,replace(address,' Nine ',' Ten ') AS rep from test_tb where id in (4,6)
summary :
I think I mentioned earlier Use IF(expr1,expr2,expr3) And CASE...WHEN...THEN...END The alias display of query results can be realized, but the difference is : These two are the alias display of the query result value as a whole , and replace You can replace and display the local string of the query result ( Output ).
3、 ... and 、 Update and replace
3.1 take address In the field “ In the east ” Replace with “ In the west ” , as follows
update test_tb set address=replace(address,' In the east ',' In the west ') where id=2
summary : Update and replace the local string in the field .
Four 、 Insert replace
4.1 take id=6 Of name The field value is changed to wokou
replace into test_tb VALUES(6,'wokou',' New Kyushu Island ',' Japan ')
summary : Go to the table “ Replace insert ” A piece of data , If there is no in the original table id=6 This data is inserted as new data ( amount to insert into effect ); If there is... In the original table id=6 This data is replaced ( amount to update effect ). For fields that are not specified, the default value is inserted .
边栏推荐
- DDL language of MySQL database: create, modify alter, delete drop of databases and tables
- [testing theory] thinking about testing profession
- [Galaxy Kirin V10] [server] set time synchronization of intranet server
- MBG combat zero basis
- 2、 Operators and branches
- Remove linked list elements
- Dichotomy search (C language)
- Const's constant member function after the function; Form, characteristics and use of inline function
- XMIND installation
- VPS安装Virtualmin面板
猜你喜欢
Using Lua to realize 99 multiplication table
Dynamic memory management
F12 clear the cookies of the corresponding web address
[Galaxy Kirin V10] [desktop] cannot add printer
Canoe test: two ways to create CAPL test module
What is an excellent architect in my heart?
183 sets of free resume templates to help everyone find a good job
Ten key performance indicators of software applications
[Galaxy Kirin V10] [desktop and server] FRP intranet penetration
20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
随机推荐
[untitled]
I What is security testing
Heartbeat报错 attempted replay attack
Snake (C language)
Fundamentals of database operation
Fundamentals of software testing
Iterator generators and modules
[test theory] test the dimension of professional ability
[Galaxy Kirin V10] [desktop] printer
What if the book written is too popular? Author of "deep reinforcement learning" at Peking University: then open the download
R built in data set
F12 clear the cookies of the corresponding web address
Introduction to tree and binary tree
software test
How do microservices aggregate API documents? This wave of show~
JMeter Foundation
Send a request using paste raw text
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
DCL statement of MySQL Foundation
183 sets of free resume templates to help everyone find a good job