Use sql statement replace function

Alternatively replace SQL function in () using

语法
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.

Description: This parameter is the contact because I want to create a contract, divided into drafts and arraigned state, and modify the user does not know which fields are modified, so the application to replace the insertion relpace into
Usage: When one field to build a built in database the only index to ensure that the data will change if a different insertion of the same index as long as the uniqueness of the other fields and can be changed, thus very convenient

First, prepare the experimental environment

1.1 to create the table:

The CREATE TABLE test_tb(
idint (10) unsigned the NOT NULL AUTO_INCREMENT the COMMENT 'primary key increment',
namechar (30) default NULL the COMMENT 'name',
addresschar (60) default NULL the COMMENT 'address',
countrychar (200 is) default NULL the COMMENT 'Country' ,
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, replace the query

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

select *,replace(address,'区','呕') AS rep
from test_tb

2.2 The address field of the "nine" with "ten" is displayed, as follows

select *,replace(address,'九','十') AS rep
from test_tb
where id in (4,6)

Summary: Lenovo has talked to the front using the alias IF (expr1, expr2, expr3) and CASE ... WHEN ... THEN ... END display query results can be achieved,
but the difference is: the two are query results alias value do the whole display, but can be replaced replace display (output) string of the local query results.

Third, update replacement

3.1 The address field of the "East" with "the West", as follows

update test_tb set address=replace(address,'东','西') 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 into test_tb VALUES (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 data do this alternative (equivalent to update the role). For a field is not specified the default value is inserted.

Guess you like

Origin www.cnblogs.com/yuanfang0903/p/11577307.html