Replacement function replace () using the replace function replaced in SQL () uses SQL

Alternatively replace SQL function in () using

https://www.cnblogs.com/martinzhang/p/3301224.html

Syntax
REPLACE (string_expression, string_pattern, string_replacement)

parameters
string_expression string expression to be searched. string_expression can be a character or binary data type.
string_pattern is a substring to find. string_pattern can be a character or binary data type. string_pattern not be an empty string ( '').
string_replacement replacement string. string_replacement can be a character or binary data type.

Return Type
If an input parameter data type nvarchar, nvarchar is returned; otherwise REPLACE returns varchar.
If any argument is NULL, NULL is returned.

Mandarin above all, difficult to understand! Translated into the vernacular: REPLACE (String, from_str, to_str ) namely: String replaces all from_str appear to to_str.

 

First, prepare the experimental environment

1.1 to create the table:
the CREATE TABLE `test_tb` (                                                 
` id` int (10) unsigned the NOT NULL AUTO_INCREMENT the COMMENT 'primary key increment',  
`char name` (30) default NULL the COMMENT 'name',                         
` address` char (60) default NULL COMMENT 'address',                      
`country` char (200 is) the COMMENT default NULL 'States',                     
a PRIMARY KEY (` id`)                                                    
) ENGINE = MyISAM the dEFAULT the CHARSET = UTF8 the COMMENT = '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 ''China'),
( 'zhaoliu', 'Kowloon', 'Hong Kong, China'),
( 'Q7', 'California Beef', 'United States'),
( 'Wangba', 'new Kyushu', 'Japan')



 

Second, query replacing

2.1 address field in the "zone" is replaced with "Oh" appears as

select *, replace (address, 'area', 'vomit') the AS REP
from test_tb



2.2 the address field in the "nine" Alternatively to "ten" is displayed, as follows

select *, replace (address, 'nine', '10 ') aS REP
from test_tb
the WHERE in the above mentioned id (4, 6)

summary: Lenovo has talked to the front using the IF (expr1, expr2, expr3 ) and CASE ... WHEN ... tHEN ... END alias display query results can be achieved,
        but the difference is: the two are doing overall value of the query results show an alias, but you can replace a local query results Alternatively string are displayed (output).


Third, update replacement

3.1 address field in the "East" with "West", the following
update test_tb set address = replace (address , ' East', 'West') where id = 2

Summary: on the field of local strings do update replacement.

 

Fourth, insert replacement

4.1 id = name field value of 6 to wokou
Replace the VALUES INTO test_tb (6, 'wokou', 'new Kyushu', 'Japan')

Summary: to the table "is inserted in place" data a, if not the original table id = 6 this data is inserted as a new data (corresponding to insert into effect); if the original table id = 6 do this replacement data (corresponding update action). For a field is not specified the default value is inserted.

Syntax
REPLACE (string_expression, string_pattern, string_replacement)

parameters
string_expression string expression to be searched. string_expression can be a character or binary data type.
string_pattern is a substring to find. string_pattern can be a character or binary data type. string_pattern not be an empty string ( '').
string_replacement replacement string. string_replacement can be a character or binary data type.

Return Type
If an input parameter data type nvarchar, nvarchar is returned; otherwise REPLACE returns varchar.
If any argument is NULL, NULL is returned.

Mandarin above all, difficult to understand! Translated into the vernacular: REPLACE (String, from_str, to_str ) namely: String replaces all from_str appear to to_str.

 

First, prepare the experimental environment

1.1 to create the table:
the CREATE TABLE `test_tb` (                                                 
` id` int (10) unsigned the NOT NULL AUTO_INCREMENT the COMMENT 'primary key increment',  
`char name` (30) default NULL the COMMENT 'name',                         
` address` char (60) default NULL COMMENT 'address',                      
`country` char (200 is) the COMMENT default NULL 'States',                     
a PRIMARY KEY (` id`)                                                    
) ENGINE = MyISAM the dEFAULT the CHARSET = UTF8 the COMMENT = '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 ''China'),
( 'zhaoliu', 'Kowloon', 'Hong Kong, China'),
( 'Q7', 'California Beef', 'United States'),
( 'Wangba', 'new Kyushu', 'Japan')



 

Second, query replacing

2.1 address field in the "zone" is replaced with "Oh" appears as

select *, replace (address, 'area', 'vomit') the AS REP
from test_tb



2.2 the address field in the "nine" Alternatively to "ten" is displayed, as follows

select *, replace (address, 'nine', '10 ') aS REP
from test_tb
the WHERE in the above mentioned id (4, 6)

summary: Lenovo has talked to the front using the IF (expr1, expr2, expr3 ) and CASE ... WHEN ... tHEN ... END alias display query results can be achieved,
        but the difference is: the two are doing overall value of the query results show an alias, but you can replace a local query results Alternatively string are displayed (output).


Third, update replacement

3.1 address field in the "East" with "West", the following
update test_tb set address = replace (address , ' East', 'West') where id = 2

Summary: on the field of local strings do update replacement.

 

Fourth, insert replacement

4.1 将id=6的name字段值改为wokou
replace into test_tb VALUES(6,'wokou','新九州岛','日本')

总结:向表中“替换插入”一条数据,如果原表中没有id=6这条数据就作为新数据插入(相当于insert into作用);如果原表中有id=6这条数据就做替换(相当于update作用)。对于没有指定的字段以默认值插入。

Guess you like

Origin www.cnblogs.com/wfy680/p/12079052.html