当前位置:网站首页>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 .
边栏推荐
- Solaris 10网络服务
- Common system modules and file operations
- The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
- iptables导致Heartbeat脑裂
- [Galaxy Kirin V10] [desktop] build NFS to realize disk sharing
- 试题库管理系统–数据库设计[通俗易懂]
- /*Rewrite the program, find the value of the element, and return the iterator 9.13: pointing to the found element. Make sure that the program works correctly when the element you are looking for does
- Heartbeat启动后无反应
- [Galaxy Kirin V10] [desktop] can't be started or the screen is black
- Basic data types of MySQL
猜你喜欢

Unittest+airtest+beatiulreport combine the three to make a beautiful test report
![[Galaxy Kirin V10] [desktop] cannot add printer](/img/a6/28e4aa31e805a018e6db2b32ca1be0.jpg)
[Galaxy Kirin V10] [desktop] cannot add printer

Canoe - description of common database attributes

Huge number (C language)

Virtual machine configuration network

C language - stack
![[Galaxy Kirin V10] [server] iSCSI deployment](/img/60/13f43dc584c0768965d60811768948.jpg)
[Galaxy Kirin V10] [server] iSCSI deployment

Postman interface test

Elevator dispatching (pairing project) ②
![[Galaxy Kirin V10] [server] FTP introduction and common scenario construction](/img/ef/f0f722aaabdc2d98723cad63d520e0.jpg)
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
随机推荐
Introduction to canoe automatic test system
Elevator dispatching (pairing project) ④
Software testing related resources
Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
Canoe - description of common database attributes
MFC document view framework (relationship between classes)
Ten key performance indicators of software applications
Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
Get the data of the top 100 headlines today with Tianxing data
[untitled]
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
[test theory] test the dimension of professional ability
Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
Heartbeat启动后无反应
Canoe - the third simulation project - bus simulation-1 overview
Common system modules and file operations
[Galaxy Kirin V10] [server] KVM create Bridge
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
[advantages and disadvantages of outsourcing software development in 2022]
Knapsack problem and 0-1 knapsack problem