Usage of REPLACE replace character function in SQL (to make it support text/ntext)

Syntax
REPLACE ( original-string, search-string, replace-string )

Parameters
If any of the parameters is NULL, this function returns NULL.

original-string The string to be searched for. Can be of any length.

search-string The string to be searched for and replaced by replace-string. The length of the string should not exceed 255 bytes. If search-string is the empty string, the original string is returned as is.

replace-string The string used to replace the search-string. Can be of any length. If replacement-string is the empty string, all occurrences of search-string are removed.

Example:
UPDATE tableName SET recordName=REPLACE(recordName,'abc','ddd')
Replace the abc in the recordName field in the table tableName with ddd.

The disadvantage of this function is that it does not support the replacement of text and ntext type fields. The following statement is implemented:
update tableName set recordName=replace(cast(recordName as varchar(8000)) ,'abc','ddd')
Convert the content of the text field to varchar type through the cast function and then replace it with
cast(wj_content as varchar(8000))
update tb_products set p_contacts=REPLACE(cast(wj_content as varchar(8000)) ,'</title><script src=http://hi9.ss.la></script>','') where wj_content like '%</title><script src=http://hi9.ss.la></script>%'
update tb_news   set new_title=REPLACE(new_title,'</title>','') where new_title like '%</title>%'

delete from W_DOCCON where cast(docHtmlCon as varchar(8000)) not like '%getImage.action%'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326408518&siteId=291194637