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


Answers:


151

REPLACE的语法:

替换(string_expression,string_pattern,string_replacement)

这样您需要的SQL应该是:

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

1
就我而言,我需要将双引号替换为单引号。我用下面的REPLACE([ColumnValue],““”,‘’‘’)的端部是四个单引号。
丹尼尔巴特勒

11

如果有人在乎,请NTEXT使用以下格式:

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

4
该语句本身不会执行更新。您需要执行以下操作:更新[DataTable] SET SET [ColumnValue] = CAST(REPLACE(CAST([ColumnValue] AS NVARCHAR(MAX)),'domain2','domain1')AS NTEXT)FROM [DataTable]
Adam嘿

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.