我有一个表(SQL服务器)引用路径(UNC或其他),但现在路径将会改变。

在路径列中,我有许多记录,我只需要改变路径的一部分,而不是整个路径。我需要在每条记录中,将相同的字符串更改为新的字符串。

我如何通过简单的更新来做到这一点?


当前回答

您需要使用replace函数来替换路径。

update table_name set column_name = replace(column_name, 'oldstring', 'newstring')

这里的column_name指的是您想要更改的列。

希望它能起作用。

其他回答

UPDATE CustomReports_Ta
SET vchFilter = REPLACE(CAST(vchFilter AS nvarchar(max)), '\\Ingl-report\Templates', 'C:\Customer_Templates')
where CAST(vchFilter AS nvarchar(max)) LIKE '%\\Ingl-report\Templates%'

没有CAST函数,我得到了一个错误

参数数据类型ntext对于replace函数的参数1无效。

您可以使用这个查询

update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')

如果目标列类型不是varchar/nvarchar like text,我们需要将列值转换为字符串,然后将其转换为:

update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'

很简单:

update my_table
set path = replace(path, 'oldstring', 'newstring')