string 更新中的 T-SQL 字符串替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4350482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:54:21  来源:igfitidea点击:

T-SQL string replace in Update

stringtsqlreplace

提问by Sekhar

I need to update the values of a column, with a substring replace being done on the existing values.

我需要更新列的值,并对现有值进行子字符串替换。

Example:

例子:

Data contains abc@domain1, pqr@domain2etc.

数据包括abc@domain1pqr@domain2等等。

I need to update the values such that @domain2is replaced with @domain1.

我需要更新@domain2替换为@domain1.

回答by Kofi Sarfo

The syntax for REPLACE:

REPLACE的语法:

REPLACE (string_expression,string_pattern,string_replacement)

替换(字符串表达式,字符串模式,字符串替换)

So that the SQL you need should be:

所以你需要的 SQL 应该是:

UPDATE [DataTable] SET [ColumnValue] = REPLACE([ColumnValue], 'domain2', 'domain1')

回答by Tawani

If anyone cares, for NTEXT, use the following format:

如果有人关心,对于NTEXT,请使用以下格式:

SELECT CAST(REPLACE(CAST([ColumnValue] AS NVARCHAR(MAX)),'find','replace') AS NTEXT) 
    FROM [DataTable]

回答by Joe Stefanelli

update YourTable
    set YourColumn = replace(YourColumn, '@domain2', '@domain1')
    where charindex('@domain2', YourColumn) <> 0